Skip to content

Instantly share code, notes, and snippets.

@alehano
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alehano/11144804 to your computer and use it in GitHub Desktop.
Save alehano/11144804 to your computer and use it in GitHub Desktop.
Random string in Go
import (
"crypto/rand"
)
func randString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
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