u
This commit is contained in:
1 parent
500fab7a80
commit
8f3f6982db
54 files changed
+2076
-1877
No files matched your search
@@ -6,17 +6,22 @@ import (
|
||||
)
|
||||
|
||||
// GenerateCode 生成指定长度的随机邀请码(大写字母+数字)
|
||||
func GenerateCode(length int) (string, error) {
|
||||
// 不会返回错误,如果随机失败,会自动重试
|
||||
func GenerateCode(length int) string {
|
||||
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
code := make([]byte, length)
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
for {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
// 随机失败,重试
|
||||
continue
|
||||
}
|
||||
code[i] = charset[num.Int64()]
|
||||
break
|
||||
}
|
||||
code[i] = charset[num.Int64()]
|
||||
}
|
||||
|
||||
return string(code), nil
|
||||
return string(code)
|
||||
}
|
||||
Reference in new issue
Block a user