uriuniq
is a Go package for generating unique, URI-safe strings, ideal for session tokens, database identifiers, and other high-entropy needs. It supports customizable lengths and character sets, ensuring collision resistance and URI compatibility.
- Customizable Length: Tailor the string length to fit your requirements.
- Character Set Options: Choose from numeric, lowercase, uppercase, alphanumeric, or custom sets (allow a maximum of 256 chars).
- Collision Resistance: Reduces the chance of generating duplicate identifiers.
Install uriuniq
with:
go get github.com/laofun/uriuniq
Generate a random string:
package main
import (
"fmt"
"log"
"github.com/laofun/uriuniq"
)
func main() {
opts := uriuniq.NewOpts() // Default settings
opts.Length = 20 // Custom length
result, err := uriuniq.Generate(opts)
if err != nil {
log.Fatalf("Error: %s", err)
}
fmt.Println("Generated:", result)
}
Customize the character set by disabling specific features:
opts := uriuniq.NewOpts()
opts.ExcludeNumeric = true // Exclude numeric chars
opts.ExcludeUppercase = false // Exclude uppercase chars
opts.ExcludeLowercase = true // Exclude lowercase chars
result, err := uriuniq.Generate(opts)
if err != nil {
log.Fatalf("Error: %s", err)
}
fmt.Println("Result:", result)
Specify a custom character set:
opts := uriuniq.NewOpts()
opts.CustomCharset = "abc123" // Custom character set
opts.Length = 15
result, err := uriuniq.Generate(opts)
if err != nil {
log.Fatalf("Error: %s", err)
}
fmt.Println("Generated with custom charset:", result)
Note: While uriuniq
supports custom character sets, ensure they are URI-safe to avoid compatibility issues. Non-URI-safe characters can lead to errors in URLs.
Contributions are welcome!
uriuniq
is MIT licensed. See the LICENSE file for details.