Fast, flexible random string generation for Go — ids, tokens, and passwords in one small, dependency-free package.
- 🔤 Flexible character sets — seven built-in universes, or bring your own (Unicode included).
- 🔒 Crypto-secure mode — draw from
crypto/randfor passwords and security tokens. - 🔁 Unique batches — generate thousands of guaranteed-distinct strings in one call.
- 🎲 Deterministic output — pin a
Seedfor reproducible results in tests. - 🚀 Fast — an ASCII fast path with zero dependencies beyond the standard library.
go get github.com/gbbocchini/go-randomstringpackage main
import (
"fmt"
"github.com/gbbocchini/go-randomstring"
)
func main() {
r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigits,
Length: 13,
}
id, err := r.GenerateOne()
if err != nil {
panic(err)
}
fmt.Println(id)
}r := randomstring.Randomizer{
Universe: randomstring.LowerLetters,
Length: 8,
}
id, err := r.GenerateOne()Set Unique to guarantee every string in a batch is distinct:
r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigits,
Length: 8,
Unique: true,
}
ids, err := r.Generate(10_000) // 10,000 distinct idsr := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigitsSymbols,
Length: 32,
Secure: true,
}
password, err := r.GenerateOne()r := randomstring.Randomizer{
Universe: randomstring.LowerUpperDigits,
Length: 13,
Seed: 1,
}
id, _ := r.GenerateOne() // always "9g14r5YgIsx9v"r := randomstring.Randomizer{
Universe: "ABC123", // only these characters
Length: 6,
}| Constant | Contents |
|---|---|
LowerLetters |
a-z |
UpperLetters |
A-Z |
Digits |
0-9 |
Symbols |
!@#$%&*()-_+={};:., |
LowerUpperLetters |
a-z + A-Z |
LowerUpperDigits |
a-z + A-Z + 0-9 |
LowerUpperDigitsSymbols |
a-z + A-Z + 0-9 + symbols |
Full API reference is available on pkg.go.dev.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change, and update tests as appropriate.
MIT © Gabriel Bocchini