u
This commit is contained in:
1 parent
0ab2796dd4
commit
f7b0c6ad2f
26 files changed
+1893
-354
No files matched your search
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// GenerateCode 生成指定长度的随机邀请码(大写字母+数字)
|
||||
func GenerateCode(length int) (string, error) {
|
||||
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
|
||||
}
|
||||
code[i] = charset[num.Int64()]
|
||||
}
|
||||
|
||||
return string(code), nil
|
||||
}
|
||||
Reference in new issue
Block a user