I was tired of so many Go apps and libraries being modulo-biased when generating random strings. Here's another library to generate cryptographically secure and unbiased strings. Internally this library uses Go's crypto/rand package which reads from the best random generator your system provides.
# Install library
go get -u github.com./leonklingele/randomstring/v2
// .. and use it. Generate a 30 character long string
s, err := randomstring.Generate(30, randomstring.CharsASCII)
// You can even use your own alphabet "abc". Be careful though!
s, err := randomstring.Generate(30, "abc")
Supported alphabets:
randomstring.CharsNum
: Contains numbers from 0-9randomstring.CharsAlpha
: Contains the full English alphabet: letters a-z and A-Zrandomstring.CharsAlphaNum
: Is a combination of CharsNum and CharsAlpharandomstring.CharsASCII
: Contains all printable ASCII characters in code range [32, 126]