golang随机生成指定位数的字符串
package stringx
import (
"encoding/hex"
"math/rand"
"time"
)
var letters = []byte("abcdefghjkmnpqrstuvwxyz123456789")
var longLetters = []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") // 61
func init() {
rand.Seed(time.Now().Unix())
}
// RandLow 随机字符串,包含 1~9 和 a~z - [i,l,o]
func RandLow(n int) []byte {
if n <= 0 {
return []byte{}
}
b := make([]byte, n)
arc := uint8(0)
if _, err := rand.Read(b[:]); err != nil {
return []byte{}
}
for i, x := range b {
arc = x & 31
b[i] = letters[arc]
}
return b
}
// RandUp 随机字符串,包含 英文字母和数字附加=_两个符号
func RandUp(n int) []byte {
if n <= 0 {
return []byte{}
}
b := make([]byte, n)
arc := uint8(0)
if _, err := rand.Read(b[:]); err != nil {
return []byte{}
}
for i, x := range b {
arc = x & 61
b[i] = longLetters[arc]
}
return b
}
// RandHex 生成16进制格式的随机字符串
func RandHex(n int) []byte {
if n <= 0 {
return []byte{}
}
var need int
if n & 1 == 0 { // even
need = n
} else { // odd
need = n + 1
}
size := need / 2
dst := make([]byte, need)
src := dst[size:]
if _, err := rand.Read(src[:]); err != nil {
return []byte{}
}
hex.Encode(dst, src)
return dst[:n]
}