51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package cf
|
|
|
|
import (
|
|
"math/rand"
|
|
"regexp"
|
|
)
|
|
|
|
// 是否是手机号
|
|
func IsMobile(str string) bool {
|
|
return regexp.MustCompile(`^1[3-9]\d{9}$`).MatchString(str)
|
|
}
|
|
|
|
// 随机字符串
|
|
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
// 获取随机字符串
|
|
func StrRand(strLen int) string {
|
|
randBytes := make([]rune, strLen)
|
|
for i := range randBytes {
|
|
randBytes[i] = letters[rand.Intn(len(letters))]
|
|
}
|
|
return string(randBytes)
|
|
}
|
|
|
|
// 随机字母字符串
|
|
var charLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
// 随机字母字符串
|
|
func StrCharRand(strLen int) string {
|
|
randBytes := make([]rune, strLen)
|
|
for i := range randBytes {
|
|
randBytes[i] = charLetters[rand.Intn(len(charLetters))]
|
|
}
|
|
return string(randBytes)
|
|
}
|
|
|
|
// 随机数字字符串
|
|
// 第一位不能是0
|
|
func StrNumberRand(strLen int) string {
|
|
numberLetters := []rune("0123456789")
|
|
randBytes := make([]rune, strLen)
|
|
for i := range randBytes {
|
|
if i == 0 {
|
|
randBytes[i] = letters[rand.Intn(9)]
|
|
} else {
|
|
randBytes[i] = letters[rand.Intn(len(numberLetters))]
|
|
}
|
|
}
|
|
return string(randBytes)
|
|
}
|