|
|
@@ -5,10 +5,17 @@ import (
|
|
|
"crypto/rand"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
+ mathrand "math/rand/v2"
|
|
|
"reflect"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+type helper struct{}
|
|
|
+
|
|
|
+func (h helper) Uint64() uint64 {
|
|
|
+ return uint64(time.Now().UnixNano())
|
|
|
+}
|
|
|
+
|
|
|
// Empty 类似于 PHP 的 empty() 函数
|
|
|
func Empty(val any) bool {
|
|
|
if val == nil {
|
|
|
@@ -55,6 +62,17 @@ func RandomNumber(length int) string {
|
|
|
return string(b)
|
|
|
}
|
|
|
|
|
|
+// RandomString 生成长度为 length 的随机字符串
|
|
|
+func RandomString(length int) string {
|
|
|
+ mathrand.New(&helper{})
|
|
|
+ letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
+ b := make([]byte, length)
|
|
|
+ for i := range b {
|
|
|
+ b[i] = letters[mathrand.IntN(len(letters))]
|
|
|
+ }
|
|
|
+ return string(b)
|
|
|
+}
|
|
|
+
|
|
|
// FirstElement 获取 args[0],避免 panic
|
|
|
func FirstElement(args []string) string {
|
|
|
if len(args) > 0 {
|