16 lines
310 B
Go
16 lines
310 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
)
|
|
|
|
// GenerateRandomString 生成随机字符串
|
|
func GenerateRandomString(length int) (string, error) {
|
|
bytes := make([]byte, length/2+1)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(bytes)[:length], nil
|
|
}
|