Created
August 12, 2020 15:25
-
-
Save girishramnani/6423bbb8313354dd75912ed97a9d5e8a to your computer and use it in GitHub Desktop.
Random string generation
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
import ( | |
"crypto/rand" | |
) | |
func GenerateRandomString(n int) string { | |
b := make([]rune, n) | |
letterRunes := []rune("abcdefghijklmnopqrstuvwxyz") | |
for i := range b { | |
// this error is ignored because it fails only when the 2nd arg of Int() is less then 0 | |
// which wont happen | |
n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes)))) | |
b[i] = letterRunes[n.Int64()] | |
} | |
return string(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment