### Install Go Nanoid Source: https://github.com/matoous/go-nanoid/blob/main/README.md Use the go get command to install the latest version of the Go Nanoid package. ```bash go get github.com/matoous/go-nanoid/v2 ``` -------------------------------- ### Using Predefined Alphabet Constants Source: https://context7.com/matoous/go-nanoid/llms.txt Shows how to use Go Nanoid's predefined alphabet constants for common scenarios. These eliminate the need to manually define character sets. ```go package main import ( "fmt" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // AlphaNum: a-z A-Z 0-9 fmt.Println(gonanoid.MustGenerate(gonanoid.AlphaNum, 16)) // Alpha: a-z A-Z fmt.Println(gonanoid.MustGenerate(gonanoid.Alpha, 16)) // AlphaLower: a-z fmt.Println(gonanoid.MustGenerate(gonanoid.AlphaLower, 16)) // AlphaUpper: A-Z fmt.Println(gonanoid.MustGenerate(gonanoid.AlphaUpper, 16)) // AlphaLowerNum: a-z 0-9 fmt.Println(gonanoid.MustGenerate(gonanoid.AlphaLowerNum, 16)) // AlphaUpperNum: A-Z 0-9 fmt.Println(gonanoid.MustGenerate(gonanoid.AlphaUpperNum, 16)) // Numeric: 0-9 fmt.Println(gonanoid.MustGenerate(gonanoid.Numeric, 8)) // CrockfordBase32Upper: 0-9 A-Z (no I L O U) fmt.Println(gonanoid.MustGenerate(gonanoid.CrockfordBase32Upper, 20)) // CrockfordBase32Lower: 0-9 a-z (no i l o u) fmt.Println(gonanoid.MustGenerate(gonanoid.CrockfordBase32Lower, 20)) } ``` -------------------------------- ### MustGenerate IDs with Different Alphabets Source: https://context7.com/matoous/go-nanoid/llms.txt Demonstrates generating IDs using MustGenerate with built-in and custom alphabets. Useful when alphabet and size are compile-time constants. ```go package main import ( "fmt" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // Alphanumeric IDs using the built-in AlphaNum constant id := gonanoid.MustGenerate(gonanoid.AlphaNum, 21) fmt.Println(id) // e.g. "Kx3mN8pQvR2wLtYuZsAeJ" // Numeric-only token pin := gonanoid.MustGenerate(gonanoid.Numeric, 6) fmt.Println(pin) // e.g. "483920" // Crockford Base32 (no ambiguous characters) b32id := gonanoid.MustGenerate(gonanoid.CrockfordBase32Upper, 26) fmt.Println(b32id) // e.g. "7Z4KQNR2BM8SXVCJ6PH0WDTEF1" // Emoji IDs for fun emojiID := gonanoid.MustGenerate("πŸš€πŸ’©πŸ¦„πŸ€–", 4) fmt.Println(emojiID) // e.g. "πŸ¦„πŸ€–πŸš€πŸ’©" } ``` -------------------------------- ### Panic-on-Error Wrapper for New Source: https://context7.com/matoous/go-nanoid/llms.txt Calls the 'New' function and panics if an error occurs during ID generation. This is suitable for initialization or when ID generation failure is unrecoverable. Accepts an optional length argument. ```go package main import ( "fmt" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // Default 21-character ID β€” panics only if crypto/rand fails (extremely rare) id := gonanoid.Must() fmt.Println(id) // e.g. "N7eY3kP_Qm1RvTsWxUoLz" // Custom length shortID := gonanoid.Must(8) fmt.Println(shortID) // e.g. "aB3_xZ9q" } ``` -------------------------------- ### Generate ID with Custom Alphabet and Size Source: https://context7.com/matoous/go-nanoid/llms.txt Generates an ID using a custom alphabet and specified length. The alphabet can include Unicode characters and must not exceed 255 characters. Returns an error if the alphabet is empty, too long, or the size is non-positive. ```go package main import ( "fmt" "log" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // ASCII alphabet id, err := gonanoid.Generate("abcdefghijklmnopqrstuvwxyz0123456789", 16) if err != nil { log.Fatalf("failed to generate id: %v", err) } fmt.Println(id) // e.g. "k3z9w1mq8vbn4xyt" // Hex-only IDs hexID, err := gonanoid.Generate("0123456789abcdef", 32) if err != nil { log.Fatalf("failed to generate hex id: %v", err) } fmt.Println(hexID) // e.g. "3f9a1c07b4e82d560fa37b9e10cd24b8" // Unicode / emoji alphabet emojiID, err := gonanoid.Generate("πŸš€πŸ’©πŸ¦„πŸ€–", 8) if err != nil { log.Fatalf("failed to generate emoji id: %v", err) } fmt.Println(emojiID) // e.g. "πŸ€–πŸš€πŸ¦„πŸ€–πŸš€πŸ¦„πŸ’©" // Multi-script alphabet multiID, err := gonanoid.Generate("こけんにабдСТиклは你ε₯½ε–‚Χ©ΧœΧ•Χ", 10) if err != nil { log.Fatalf("failed to generate multi-script id: %v", err) } fmt.Println(multiID) // Alphabet too long β€” returns error _, err = gonanoid.Generate(string(make([]byte, 256)), 10) fmt.Println(err) // "alphabet must not be empty and contain no more than 255 chars" } ``` -------------------------------- ### `Must` β€” Panic-on-error wrapper for `New` Source: https://context7.com/matoous/go-nanoid/llms.txt A convenience function that wraps `New` and panics if an error occurs during ID generation. It is suitable for use during program initialization or in scenarios where ID generation failure is considered unrecoverable. It accepts the same optional length argument as `New`. ```APIDOC ## `Must` β€” Panic-on-error wrapper for `New` ### Description Calls `New` and panics if an error occurs. Intended for program initialization or cases where a failed ID generation is unrecoverable. Accepts the same optional length argument as `New`. ### Method Signature `func Must(length ...int) string` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **length** (int) - Optional - The desired length of the generated ID. Defaults to 21 if not provided. ### Request Example ```go package main import ( "fmt" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // Default 21-character ID β€” panics only if crypto/rand fails (extremely rare) id := gonanoid.Must() fmt.Println(id) // e.g. "N7eY3kP_Qm1RvTsWxUoLz" // Custom length shortID := gonanoid.Must(8) fmt.Println(shortID) // e.g. "aB3_xZ9q" } ``` ### Response #### Success Response - **id** (string) - The generated unique ID. #### Error Response This function panics on error and does not return an error value. ``` -------------------------------- ### Generate URL-safe ID with Default Alphabet Source: https://context7.com/matoous/go-nanoid/llms.txt Generates a secure, URL-friendly unique ID using the default alphabet. Accepts an optional integer to specify the ID length. Returns an error for negative lengths or random source failures. ```go package main import ( "fmt" "log" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // Default length: 21 characters id, err := gonanoid.New() if err != nil { log.Fatalf("failed to generate id: %v", err) } fmt.Println(id) // e.g. "V1StGXR8_Z5jdHi6B-myT" // Custom length: 10 characters shortID, err := gonanoid.New(10) if err != nil { log.Fatalf("failed to generate id: %v", err) } fmt.Println(shortID) // e.g. "irxkHFAu3_" // Negative length returns an error _, err = gonanoid.New(-1) fmt.Println(err) // "negative id length" } ``` -------------------------------- ### `New` β€” Generate a URL-safe ID with the default alphabet Source: https://context7.com/matoous/go-nanoid/llms.txt Generates a secure, URL-friendly unique ID using the default alphabet. It can produce IDs of a default 21-character length or a custom length specified by an integer argument. Returns an error for negative lengths or random source failures. ```APIDOC ## `New` β€” Generate a URL-safe ID with the default alphabet ### Description Generates a secure, URL-friendly unique ID using the default alphabet (`_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`). Without arguments it returns a 21-character ID; passing a single integer overrides the length. Returns an error if the length is negative or if the random source fails. ### Method Signature `func New(length ...int) (string, error)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```go package main import ( "fmt" "log" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // Default length: 21 characters id, err := gonanoid.New() if err != nil { log.Fatalf("failed to generate id: %v", err) } fmt.Println(id) // e.g. "V1StGXR8_Z5jdHi6B-myT" // Custom length: 10 characters shortID, err := gonanoid.New(10) if err != nil { log.Fatalf("failed to generate id: %v", err) } fmt.Println(shortID) // e.g. "irxkHFAu3_" // Negative length returns an error _, err = gonanoid.New(-1) fmt.Println(err) // "negative id length" } ``` ### Response #### Success Response - **id** (string) - The generated unique ID. #### Error Response - **error** (error) - An error if ID generation fails (e.g., negative length, random source failure). ### Response Example ```json { "id": "V1StGXR8_Z5jdHi6B-myT" } ``` ``` -------------------------------- ### Generate Default ID Source: https://github.com/matoous/go-nanoid/blob/main/README.md Generate a unique ID using the default alphabet and length provided by the library. Ensure the gonanoid package is imported. ```go id, err := gonanoid.New() ``` -------------------------------- ### `Generate` β€” Generate an ID with a custom alphabet and size Source: https://context7.com/matoous/go-nanoid/llms.txt A low-level function that generates an ID using a specified alphabet and length. It supports custom alphabets, including Unicode and emoji characters, with a maximum of 255 characters. Returns an error if the alphabet is empty, too long, or if the specified size is non-positive. ```APIDOC ## `Generate` β€” Generate an ID with a custom alphabet and size ### Description Low-level function that accepts any alphabet (up to 255 characters, including Unicode/emoji) and a target length. Uses a bitmask-based rejection-sampling algorithm to guarantee a flat character distribution. Returns an error when the alphabet is empty, exceeds 255 characters, or the size is non-positive. ### Method Signature `func Generate(alphabet string, size int) (string, error)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **alphabet** (string) - Required - The set of characters to use for generating the ID. Maximum 255 characters. - **size** (int) - Required - The desired length of the generated ID. Must be a positive integer. ### Request Example ```go package main import ( "fmt" "log" gonanoid "github.com/matoous/go-nanoid/v2" ) func main() { // ASCII alphabet id, err := gonanoid.Generate("abcdefghijklmnopqrstuvwxyz0123456789", 16) if err != nil { log.Fatalf("failed to generate id: %v", err) } fmt.Println(id) // e.g. "k3z9w1mq8vbn4xyt" // Hex-only IDs hexID, err := gonanoid.Generate("0123456789abcdef", 32) if err != nil { log.Fatalf("failed to generate hex id: %v", err) } fmt.Println(hexID) // e.g. "3f9a1c07b4e82d560fa37b9e10cd24b8" // Unicode / emoji alphabet emojiID, err := gonanoid.Generate("πŸš€πŸ’©πŸ¦„πŸ€–", 8) if err != nil { log.Fatalf("failed to generate emoji id: %v", err) } fmt.Println(emojiID) // e.g. "πŸ€–πŸš€πŸ¦„πŸ€–πŸš€πŸ¦„πŸ’©" // Multi-script alphabet multiID, err := gonanoid.Generate("こけんにабдСТиклは你ε₯½ε–‚Χ©ΧœΧ•Χ", 10) if err != nil { log.Fatalf("failed to generate multi-script id: %v", err) } fmt.Println(multiID) // Alphabet too long β€” returns error _, err = gonanoid.Generate(string(make([]byte, 256)), 10) fmt.Println(err) // "alphabet must not be empty and contain no more than 255 chars" } ``` ### Response #### Success Response - **id** (string) - The generated unique ID. #### Error Response - **error** (error) - An error if ID generation fails (e.g., invalid alphabet, non-positive size). ### Response Example ```json { "id": "k3z9w1mq8vbn4xyt" } ``` ``` -------------------------------- ### Generate Custom ID Source: https://github.com/matoous/go-nanoid/blob/main/README.md Generate a unique ID with a specified custom alphabet and length. This allows for greater control over the ID's characteristics. ```go id, err := gonanoid.Generate("abcde", 54) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.