Skip to content

Instantly share code, notes, and snippets.

@girishramnani
Created August 12, 2020 15:25
Show Gist options
  • Save girishramnani/6423bbb8313354dd75912ed97a9d5e8a to your computer and use it in GitHub Desktop.
Save girishramnani/6423bbb8313354dd75912ed97a9d5e8a to your computer and use it in GitHub Desktop.
Random string generation
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