23 lines
472 B
Go
23 lines
472 B
Go
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
|
|
}
|