Install Go Nanoid v2
mainInstall the go-nanoid package using the go get tool. Ensure you use the /v2 suffix to pull the correct major version.
$ go get github.com/matoous/go-nanoid/v2repository·main·Indexed 23 days ago
https://github.com/matoous/go-nanoidA Go implementation of the NanoID library for generating fast, compact, and cryptographically secure unique IDs. It provides functions like New() for default URL-friendly IDs and Generate() for custom alphabets and lengths, along with Must() and MustGenerate() variants that panic on error.
Install the go-nanoid package using the go get tool. Ensure you use the /v2 suffix to pull the correct major version.
$ go get github.com/matoous/go-nanoid/v2Use gonanoid.Generate(alphabet string, size int) to create an ID with a specific set of characters and a specific length.
id, err := gonanoid.Generate("abcde", 54)Use gonanoid.New() to generate a cryptographically secure, compact ID using the default alphabet and length.
id, err := gonanoid.New()Use New() to generate a secure, URL-friendly unique ID using the default alphabet (_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ).
You can optionally provide a single integer argument to specify the desired length of the ID. If no argument is provided, it defaults to a length of 21.
Errors:
Use Generate(alphabet string, size int) when you need to use a specific set of characters and a specific ID length. This is a low-level function that allows for full customization of the ID generation process.
Errors:
If you prefer to avoid manual error checking and are confident in your parameters, use the Must variants. These functions will panic if an error occurs during generation.
Must(...int) is the panic-equivalent of New().MustGenerate(alphabet string, size int) is the panic-equivalent of Generate().The package provides several predefined alphabets for common use cases:
| Name | Description |
|---|---|
AlphaNum | Alpha-numerical characters (abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789) |
Alpha | Upper and lower case letters (abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) |
AlphaLowerNum | Lower case letters and numbers (abcdefghijklmnopqrstuvwxyz0123456789) |
AlphaUpperNum | Upper case letters and numbers (ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789) |
AlphaLower | Lower case letters (abcdefghijklmnopqrstuvwxyz) |
AlphaUpper | Upper case letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ) |
Numeric | Numerical characters (0123456789) |
CrockfordBase32Upper | Crockford uppercase base32 (0123456789ABCDEFGHJKMNPQRSTVWXYZ) |
CrockfordBase32Lower | Crockford lower base32 (0123456789abcdefghjkmnpqrstvwxyz) |