randstr is a module that contains functions for generating random strings.
The functions in this module uses the crypto/rand
package.
go get github.com/henrikac/randstr
The strings generated by Generate
and GenerateLen
are all unbiased, meaning that each of the characters in the character set a-zA-Z0-9
(62 characters) has an equal chance of being used in the generated string.
randstr.Generate
returns a random string that is 16 characters long.
package main
import (
"fmt"
"log"
"github.com/henrikac/randstr"
)
func main() {
str, err := randstr.Generate()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", str)
}
// Example output:
// 4a1Lz3d4yXihvbJX
randstr.GenerateLen
returns a random string of the specified length.
package main
import (
"fmt"
"log"
"github.com/henrikac/randstr"
)
func main() {
str, err := randstr.GenerateLen(20)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", str)
}
// Example output:
// bRXVtwgaLVsBuYJtP9Lf
randstr.Base64Encoded
returns a random base64 encoded string.
package main
import (
"fmt"
"log"
"github.com/henrikac/randstr"
)
func main() {
str, err := randstr.Base64Encoded()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", str)
}
// Example output:
// M72WQsavclA/wLYfxuyr2Q==
- Fork it (https://github.com/henrikac/randstr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Henrik Christensen - creator and maintainer