Created
June 20, 2014 21:23
-
-
Save rickt/88b791041d23c0b38644 to your computer and use it in GitHub Desktop.
generate a random string
This file contains 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
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