Skip to content

Commit 554e6f8

Browse files
committed
feat: Add Must functions
Add MustGenerate and MustID functions that panic on error instead of returning it. Fixes: #17
1 parent 53c5d4a commit 554e6f8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

gonanoid.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,21 @@ func Nanoid(param ...int) (string, error) {
111111
func ID(l int) (string, error) {
112112
return Nanoid(l)
113113
}
114+
115+
// MustID is the same as ID but panics on error.
116+
func MustID(l int) string {
117+
id, err := Nanoid(l)
118+
if err != nil {
119+
panic(err)
120+
}
121+
return id
122+
}
123+
124+
// MustGenerate is the same as Generate but panics on error.
125+
func MustGenerate(rawAlphabet string, size int) string {
126+
id, err := Generate(rawAlphabet, size)
127+
if err != nil {
128+
panic(err)
129+
}
130+
return id
131+
}

0 commit comments

Comments
 (0)