From 554e6f88855b4aaff656b9e1d9dee41c96b5d764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Thu, 8 Oct 2020 21:43:36 +0200 Subject: [PATCH] feat: Add Must functions Add MustGenerate and MustID functions that panic on error instead of returning it. Fixes: https://github.com/matoous/go-nanoid/issues/17 --- gonanoid.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gonanoid.go b/gonanoid.go index 2440cd1..02ed538 100644 --- a/gonanoid.go +++ b/gonanoid.go @@ -111,3 +111,21 @@ func Nanoid(param ...int) (string, error) { func ID(l int) (string, error) { return Nanoid(l) } + +// MustID is the same as ID but panics on error. +func MustID(l int) string { + id, err := Nanoid(l) + if err != nil { + panic(err) + } + return id +} + +// MustGenerate is the same as Generate but panics on error. +func MustGenerate(rawAlphabet string, size int) string { + id, err := Generate(rawAlphabet, size) + if err != nil { + panic(err) + } + return id +}