randstr.go (441B)
1 package randstr 2 3 import ( 4 "math/rand" 5 "time" 6 ) 7 8 const ( 9 Alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 10 Numeric string = "0123456789" 11 ) 12 13 var random = rand.New(rand.NewSource(time.Now().UnixNano())) 14 15 func Charset(charset string, length int) string { 16 var b = make([]byte, length) 17 18 random.Read(b) 19 20 for i := 0; i < length; i++ { 21 b[i] = charset[int(b[i])%len(charset)] 22 } 23 24 return string(b) 25 }