### Manage Default ShortID Generator Source: https://context7.com/teris-io/shortid/llms.txt Allows setting and getting the default ShortID generator. Configure the generator once at startup and use the simple Generate() function throughout your application. GetDefault retrieves the current default instance. ```go package main import ( "fmt" "github.com/teris-io/shortid" ) func main() { // Get the current default generator defaultSid := shortid.GetDefault() fmt.Println("Default generator:", defaultSid.String()) // Output: Shortid(worker=0, epoch=2016-01-01 00:00:00 +0000 UTC, abc=Abc{alphabet='gzmZM7VINvOFcpho01x-fYPs8Q_urjq6RkiWGn4SHDdK5t2TAJbaBLEyUwlX9C3e'}) // Create and set a new default generator for worker 5 customSid := shortid.MustNew(5, shortid.DefaultABC, 12345) shortid.SetDefault(customSid) // Now Generate() uses the new default id := shortid.MustGenerate() fmt.Println("ID from custom default:", id) // Verify the default was changed newDefault := shortid.GetDefault() fmt.Println("Worker:", newDefault.Worker()) // Output: Worker: 5 fmt.Println("Epoch:", newDefault.Epoch()) // Output: Epoch: 2016-01-01 00:00:00 +0000 UTC } ``` -------------------------------- ### Generate Short ID Source: https://context7.com/teris-io/shortid/llms.txt Generates a new short ID using the default generator. This is the simplest way to start generating IDs without any configuration. Returns a unique, URL-friendly string identifier. ```APIDOC ## Generate Short ID ### Description Generates a new short ID using the default generator. Returns a unique, URL-friendly string identifier. ### Method `Generate()` ### Parameters None ### Request Example ```go package main import ( "fmt" "log" "github.com/teris-io/shortid" ) func main() { id1, err := shortid.Generate() if err != nil { log.Fatal(err) } fmt.Println("Generated ID:", id1) id2, err := shortid.Generate() if err != nil { log.Fatal(err) } fmt.Println("Generated ID:", id2) id3 := shortid.MustGenerate() fmt.Println("Generated ID:", id3) } ``` ### Response #### Success Response (string) - Returns a unique, URL-friendly string identifier. #### Response Example ``` Generated ID: -NDveu-9Q Generated ID: iNove6iQ9J Generated ID: NVDve6-9Q ``` ``` -------------------------------- ### Initialize and Use a Custom Generator Source: https://github.com/teris-io/shortid/blob/master/README.md Recommended for performance and control. Initialize a new generator with a worker ID, alphabet, and seed, then use its Generate method. ```go sid, err := shortid.New(1, shortid.DefaultABC, 2342) // then either: fmt.Printf(sid.Generate()) fmt.Printf(sid.Generate()) ``` -------------------------------- ### Set Default Generator and Use Source: https://github.com/teris-io/shortid/blob/master/README.md After initializing a custom generator, you can set it as the default. Subsequent calls to the package-level Generate function will use this custom generator. ```go shortid.SetDefault(sid) // followed by: fmt.Printf(shortid.Generate()) fmt.Printf(shortid.Generate()) ``` -------------------------------- ### ShortID Instance Methods for Configuration Source: https://context7.com/teris-io/shortid/llms.txt Accesses configuration details of a ShortID generator instance, including the alphabet, worker ID, and epoch timestamp. Useful for debugging and verification. Generates sample IDs using the instance. ```go package main import ( "fmt" "github.com/teris-io/shortid" ) func main() { // Create a generator with specific configuration sid := shortid.MustNew(25, shortid.DefaultABC, 1) // Access configuration via instance methods fmt.Println("Worker ID:", sid.Worker()) // Output: Worker ID: 25 fmt.Println("Epoch:", sid.Epoch()) // Output: Epoch: 2016-01-01 00:00:00 +0000 UTC fmt.Println("Alphabet:", sid.Abc().Alphabet()) // String representation for debugging fmt.Println("Generator:", sid.String()) // Output: Shortid(worker=25, epoch=2016-01-01 00:00:00 +0000 UTC, abc=Abc{alphabet='...'}) // Generate IDs using the instance for i := 0; i < 3; i++ { id, _ := sid.Generate() fmt.Printf("ID %d: %s (length: %d)\n", i+1, id, len(id)) } } ``` -------------------------------- ### Shortid Instance Methods Source: https://context7.com/teris-io/shortid/llms.txt Methods to access configuration of a Shortid instance. ```APIDOC ## Shortid Instance Methods ### Description Provides methods to access the configuration of a Shortid instance, including the alphabet, worker ID, and epoch. ### Methods - **Abc()** - Returns the alphabet instance. - **Worker()** - Returns the worker ID. - **Epoch()** - Returns the epoch timestamp used for ID generation. - **Generate()** - Generates a new unique ID. ``` -------------------------------- ### Accessing Default Alphabet and Version in Go Source: https://context7.com/teris-io/shortid/llms.txt Demonstrates how to access the default URL-friendly alphabet and the library version. The DefaultABC constant provides a 64-character alphabet suitable for URLs. ```go package main import ( "fmt" "github.com/teris-io/shortid" ) func main() { // DefaultABC is the standard URL-friendly alphabet fmt.Println("Default alphabet:", shortid.DefaultABC) // Output: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_- fmt.Println("Alphabet length:", len(shortid.DefaultABC)) // Output: 64 // Library version fmt.Println("Version:", shortid.Version) // Output: 1.1 } ``` -------------------------------- ### Generating IDs with Custom Seed and Alphabet in Go Source: https://context7.com/teris-io/shortid/llms.txt Shows how to create ShortID generators with custom seeds and the default alphabet for consistent shuffling. This is useful for ensuring identical shuffled alphabets across different generator instances when using the same seed. ```go package main import ( "fmt" "github.com/teris-io/shortid" ) func main() { // Use DefaultABC with custom seed for consistent shuffling across instances sid1 := shortid.MustNew(1, shortid.DefaultABC, 9999) sid2 := shortid.MustNew(2, shortid.DefaultABC, 9999) // Same seed, different worker // Both generators use the same alphabet shuffling fmt.Println("Alphabet 1:", sid1.Abc().Alphabet()) fmt.Println("Alphabet 2:", sid2.Abc().Alphabet()) // Both will output the same shuffled alphabet } ``` -------------------------------- ### Generate Short IDs Source: https://github.com/teris-io/shortid/blob/master/README.md Use this method for quick ID generation when a pre-initialized generator is not necessary. It uses the default generator. ```go fmt.Printf(shortid.Generate()) ``` ```go fmt.Printf(shortid.Generate()) ``` -------------------------------- ### Create and Use a New ShortID Generator Source: https://context7.com/teris-io/shortid/llms.txt Creates a new Shortid generator instance with a specific worker ID, alphabet, and seed. The worker number should be unique for each process. Use MustNew to panic on invalid configuration. ```go package main import ( "fmt" "log" "github.com/teris-io/shortid" ) func main() { // Create a new generator with worker ID 1, default alphabet, and seed 2342 sid, err := shortid.New(1, shortid.DefaultABC, 2342) if err != nil { log.Fatal(err) } // Generate IDs using this specific generator id1, err := sid.Generate() if err != nil { log.Fatal(err) } fmt.Println("Worker 1 ID:", id1) // Output: Worker 1 ID: VVDvc6i99J id2 := sid.MustGenerate() fmt.Println("Worker 1 ID:", id2) // Output: Worker 1 ID: NVovc6-QQy // Create another generator for a different worker sid2, err := shortid.New(2, shortid.DefaultABC, 2342) if err != nil { log.Fatal(err) } id3, _ := sid2.Generate() fmt.Println("Worker 2 ID:", id3) // Output: Worker 2 ID: tFmGc6iQQs // Use MustNew when you want to panic on invalid configuration sid3 := shortid.MustNew(3, shortid.DefaultABC, 2342) fmt.Println("Worker 3 ID:", sid3.MustGenerate()) // Output: Worker 3 ID: KpTvcui99k } ``` -------------------------------- ### Generate ShortID using Default Generator Source: https://context7.com/teris-io/shortid/llms.txt Generates a new short ID using the default generator. Use this for the simplest way to generate IDs without configuration. Panics on error if MustGenerate is used. ```go package main import ( "fmt" "log" "github.com/teris-io/shortid" ) func main() { // Generate IDs using the default generator id1, err := shortid.Generate() if err != nil { log.Fatal(err) } fmt.Println("Generated ID:", id1) // Output: Generated ID: -NDveu-9Q id2, err := shortid.Generate() if err != nil { log.Fatal(err) } fmt.Println("Generated ID:", id2) // Output: Generated ID: iNove6iQ9J // Use MustGenerate when you want to panic on error instead id3 := shortid.MustGenerate() fmt.Println("Generated ID:", id3) // Output: Generated ID: NVDve6-9Q } ``` -------------------------------- ### Create Custom Alphabet with ShortID Source: https://context7.com/teris-io/shortid/llms.txt Generates a custom alphabet for ID generation using a seed for shuffling. The alphabet must contain exactly 64 unique characters. Use MustNewAbc for panic-on-error behavior. ```go package main import ( "fmt" "log" "github.com/teris-io/shortid" ) func main() { // Create a custom alphabet with seed 1 abc, err := shortid.NewAbc(shortid.DefaultABC, 1) if err != nil { log.Fatal(err) } fmt.Println("Shuffled alphabet:", abc.Alphabet()) // Output: Shuffled alphabet: gzmZM7VINvOFcpho01x-fYPs8Q_urjq6RkiWGn4SHDdK5t2TAJbaBLEyUwlX9C3e // Different seed produces different shuffling abc2, _ := shortid.NewAbc(shortid.DefaultABC, 345234) fmt.Println("Different shuffle:", abc2.Alphabet()) // Output: Different shuffle: U8dEc3Hnuq_RfyDApaT1ZxQmYePBCNMkF4-KJSvhjw609I7GlbzsriOL52XVoWgt // Use MustNewAbc for panic-on-error behavior abc3 := shortid.MustNewAbc(shortid.DefaultABC, 999) fmt.Println("Alphabet:", abc3.String()) // Custom alphabet must have exactly 64 unique characters customAlphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" abc4, err := shortid.NewAbc(customAlphabet, 1) if err != nil { log.Fatal(err) } fmt.Println("Custom alphabet result:", abc4.Alphabet()) } ``` -------------------------------- ### New ShortID Generator Source: https://context7.com/teris-io/shortid/llms.txt Creates a new Shortid generator instance with a specific worker ID, alphabet, and seed value. The worker number (0-31) should be unique for each process or distributed instance generating IDs into the same data space. The seed value shuffles the alphabet and should be identical across all workers. ```APIDOC ## New ShortID Generator ### Description Creates a new Shortid generator instance with a specific worker ID, alphabet, and seed value. ### Method `New(worker int, abc string, seed int64) (*Shortid, error)` `MustNew(worker int, abc string, seed int64) *Shortid` ### Parameters #### Path Parameters - **worker** (int) - Required - The worker ID (0-31). - **abc** (string) - Required - The alphabet to use for generating IDs. - **seed** (int64) - Required - The seed value to shuffle the alphabet. ### Request Example ```go package main import ( "fmt" "log" "github.com/teris-io/shortid" ) func main() { sid, err := shortid.New(1, shortid.DefaultABC, 2342) if err != nil { log.Fatal(err) } id1, err := sid.Generate() if err != nil { log.Fatal(err) } fmt.Println("Worker 1 ID:", id1) id2 := sid.MustGenerate() fmt.Println("Worker 1 ID:", id2) sid2, err := shortid.New(2, shortid.DefaultABC, 2342) if err != nil { log.Fatal(err) } id3, _ := sid2.Generate() fmt.Println("Worker 2 ID:", id3) sid3 := shortid.MustNew(3, shortid.DefaultABC, 2342) fmt.Println("Worker 3 ID:", sid3.MustGenerate()) } ``` ### Response #### Success Response (Shortid) - Returns a new Shortid generator instance. #### Response Example ``` Worker 1 ID: VVDvc6i99J Worker 1 ID: NVovc6-QQy Worker 2 ID: tFmGc6iQQs Worker 3 ID: KpTvcui99k ``` ``` -------------------------------- ### SetDefault and GetDefault Generator Source: https://context7.com/teris-io/shortid/llms.txt SetDefault replaces the default generator, allowing you to customize the worker ID and seed for your application. GetDefault retrieves the current default generator instance. This is useful when you want to configure the generator once at startup and use the simple Generate() function throughout your application. ```APIDOC ## SetDefault and GetDefault Generator ### Description Manages the default ShortID generator instance for the application. ### Methods - `GetDefault() *Shortid` - `SetDefault(sid *Shortid)` ### Parameters #### GetDefault() None #### SetDefault(sid *Shortid) - **sid** (*Shortid) - Required - The new default Shortid generator instance. ### Request Example ```go package main import ( "fmt" "github.com/teris-io/shortid" ) func main() { defaultSid := shortid.GetDefault() fmt.Println("Default generator:", defaultSid.String()) customSid := shortid.MustNew(5, shortid.DefaultABC, 12345) shortid.SetDefault(customSid) id := shortid.MustGenerate() fmt.Println("ID from custom default:", id) newDefault := shortid.GetDefault() fmt.Println("Worker:", newDefault.Worker()) fmt.Println("Epoch:", newDefault.Epoch()) } ``` ### Response #### Success Response (GetDefault) - Returns the current default Shortid generator instance. #### Success Response (SetDefault) - No return value, but the default generator is updated. #### Response Example ``` Default generator: Shortid(worker=0, epoch=2016-01-01 00:00:00 +0000 UTC, abc=Abc{alphabet='gzmZM7VINvOFcpho01x-fYPs8Q_urjq6RkiWGn4SHDdK5t2TAJbaBLEyUwlX9C3e'}) ID from custom default: KpTvcui99k Worker: 5 Epoch: 2016-01-01 00:00:00 +0000 UTC ``` ``` -------------------------------- ### Encode Numeric Value with ShortID Source: https://context7.com/teris-io/shortid/llms.txt Encodes a numeric value into a slice of runes using a shuffled alphabet. The nsymbols parameter specifies the output length (0 for auto-compute), and digits controls randomness (4-6). This is a low-level function for custom ID generation. ```go package main import ( "fmt" "log" "github.com/teris-io/shortid" ) func main() { abc := shortid.MustNewAbc(shortid.DefaultABC, 1) // Encode a small value with 2 symbols and medium randomness (digits=5) encoded, err := abc.Encode(48, 2, 5) if err != nil { log.Fatal(err) } fmt.Println("Encoded 48:", string(encoded)) // 2-character output // Encode with auto-computed length (nsymbols=0) encoded2, _ := abc.Encode(1000, 0, 4) fmt.Println("Encoded 1000 (auto length):", string(encoded2)) // Encode large value with no randomness (digits=6, most compact) encoded3, _ := abc.Encode(214235345234524356, 0, 6) fmt.Println("Encoded large value:", string(encoded3), "length:", len(encoded3)) // Output: length: 10 // Use MustEncode for panic-on-error behavior encoded4 := abc.MustEncode(25, 0, 4) fmt.Println("MustEncode result:", string(encoded4)) // Digits parameter controls randomness: // 4 = high randomness (4 possible symbols per value, up to 16 values) // 5 = medium randomness (2 possible symbols per value, up to 32 values) // 6 = no randomness (1 symbol per value, up to 64 values) for digits := uint(4); digits <= 6; digits++ { result, _ := abc.Encode(25, 0, digits) fmt.Printf("Digits=%d, Length=%d\n", digits, len(result)) } } ``` -------------------------------- ### NewAbc Source: https://context7.com/teris-io/shortid/llms.txt Creates a custom alphabet for ID generation. The alphabet must contain exactly 64 unique characters. ```APIDOC ## NewAbc ### Description Creates a custom alphabet for ID generation. The alphabet must contain exactly 64 unique characters. Different seeds produce different alphabet shufflings. ### Parameters - **alphabet** (string) - Required - The 64-character string to use for the alphabet. - **seed** (int64) - Required - The seed value for shuffling the alphabet. ### Response - **abc** (Abc) - The initialized alphabet instance. ``` -------------------------------- ### Abc.Encode Source: https://context7.com/teris-io/shortid/llms.txt Encodes a numeric value into a slice of runes using the shuffled alphabet. ```APIDOC ## Abc.Encode ### Description Encodes a numeric value into a slice of runes using the shuffled alphabet. This is a low-level function for building custom ID generation algorithms. ### Parameters - **value** (uint64) - Required - The numeric value to encode. - **nsymbols** (uint) - Required - The output length (0 for auto-compute). - **digits** (uint) - Required - Controls randomness (4-6, where 6 means no randomness). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.