写了一个简单golang生成随机字符串的方法,分享出来,生成的都是大写的字母
var r *rand.Rand
func init() {
r = rand.New(rand.NewSource(time.Now().Unix()))
}
// RandString 生成随机字符串
func RandString(len int) string {
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 65
bytes[i] = byte(b)
}
return string(bytes)
}
func TestRand(t *testing.T) {
for i := 0; i < 10; i++ {
fmt.Println(RandString(10))
}
}
测试结果:
ZGNHXBCJRQ
OWXMEWZTKK
GYEYPKJHGL
CEHJXKHRAE
FAVUCTLUMD
HTNRKJQETB
PDSSWCMSOM
ITMPQDRQIK
QZGEXAJFIR
HJICQMZRBD