Skip to content

Instantly share code, notes, and snippets.

@rickt
Created June 20, 2014 21:23
Show Gist options
  • Save rickt/88b791041d23c0b38644 to your computer and use it in GitHub Desktop.
Save rickt/88b791041d23c0b38644 to your computer and use it in GitHub Desktop.
generate a random string
package main
import (
"crypto/rand"
"fmt"
)
func main() {
fmt.Printf("randstring = %s\n", rand_str(5)) // change "5" to be whatever length string you need
}
func rand_str(str_size int) string {
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, str_size)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment