Last active
January 25, 2017 08:39
-
-
Save crgimenes/20e2c4c7d7f24beebfd4f389259daa38 to your computer and use it in GitHub Desktop.
Create random string with pre-defined size.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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