random.go 565 B

123456789101112131415161718
  1. package random
  2. import "math/rand"
  3. // Str generates a random string of the specified length using an alphanumeric character set.
  4. var Str = String([]rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
  5. // String returns a function that can be used to generate a random string of the specified length using the given character set.
  6. func String(chars []rune) func(length int) string {
  7. max := len(chars)
  8. return func(length int) string {
  9. s := make([]rune, length)
  10. for i := range s {
  11. s[i] = chars[rand.Intn(max)]
  12. }
  13. return string(s)
  14. }
  15. }