Skip to content

Instantly share code, notes, and snippets.

@crgimenes
Last active January 25, 2017 08:39
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 crgimenes/20e2c4c7d7f24beebfd4f389259daa38 to your computer and use it in GitHub Desktop.
Save crgimenes/20e2c4c7d7f24beebfd4f389259daa38 to your computer and use it in GitHub Desktop.
Create random string with pre-defined size.
import "crypto/rand"
func RandStr(strSize int) (ret string, err error) {
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, strSize)
_, err = rand.Read(bytes)
if err != nil {
return
}
for k, v := range bytes {
bytes[k] = alphanum[v%byte(len(alphanum))]
}
ret = string(bytes)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment