|
@@ -1,6 +1,10 @@
|
|
|
package util
|
|
|
|
|
|
-import "unicode/utf8"
|
|
|
+import (
|
|
|
+ "math/rand"
|
|
|
+ "time"
|
|
|
+ "unicode/utf8"
|
|
|
+)
|
|
|
|
|
|
// StrLen utf8 string length
|
|
|
func StrLen(s string) int {
|
|
@@ -33,3 +37,17 @@ func StrRange(s string, min, max int) bool {
|
|
|
}
|
|
|
return false
|
|
|
}
|
|
|
+
|
|
|
+// Random 指定长度的随机字符串(字母或数字)
|
|
|
+func Random(n int) string {
|
|
|
+ str := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
+ bytes := []byte(str)
|
|
|
+ result := []byte{}
|
|
|
+
|
|
|
+ r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
+ n1 := len(bytes)
|
|
|
+ for i := 0; i < n; i++ {
|
|
|
+ result = append(result, bytes[r.Intn(n1)])
|
|
|
+ }
|
|
|
+ return string(result)
|
|
|
+}
|