From acaaa8d6edb2e4a32f24451d08c12c2289c0e28e Mon Sep 17 00:00:00 2001 From: david Date: Wed, 21 Feb 2024 20:58:31 +0800 Subject: [PATCH] update go 1.22 --- utils/convert.go | 39 ++++++++++++++++++--------------------- utils/random.go | 27 +++++++++++++++++---------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/utils/convert.go b/utils/convert.go index 95b160a..763673b 100644 --- a/utils/convert.go +++ b/utils/convert.go @@ -2,21 +2,21 @@ package utils import ( "math" - "reflect" "strconv" "strings" - "unsafe" ) // 字符串转Int -// intStr:数字的字符串 +// +// intStr:数字的字符串 func String2Int(intStr string) (intNum int) { intNum, _ = strconv.Atoi(intStr) return } // 字符串转Int64 -// intStr:数字的字符串 +// +// intStr:数字的字符串 func String2Int64(intStr string) (int64Num int64) { intNum, _ := strconv.Atoi(intStr) int64Num = int64(intNum) @@ -24,14 +24,16 @@ func String2Int64(intStr string) (int64Num int64) { } // 字符串转Float64 -// floatStr:小数点数字的字符串 +// +// floatStr:小数点数字的字符串 func String2Float64(floatStr string) (floatNum float64) { floatNum, _ = strconv.ParseFloat(floatStr, 64) return } // 字符串转Float32 -// floatStr:小数点数字的字符串 +// +// floatStr:小数点数字的字符串 func String2Float32(floatStr string) (floatNum float32) { floatNum64, _ := strconv.ParseFloat(floatStr, 32) floatNum = float32(floatNum64) @@ -39,14 +41,16 @@ func String2Float32(floatStr string) (floatNum float32) { } // Int转字符串 -// intNum:数字字符串 +// +// intNum:数字字符串 func Int2String(intNum int) (intStr string) { intStr = strconv.Itoa(intNum) return } // Int64转字符串 -// intNum:数字字符串 +// +// intNum:数字字符串 func Int642String(intNum int64) (int64Str string) { //10, 代表10进制 int64Str = strconv.FormatInt(intNum, 10) @@ -54,8 +58,9 @@ func Int642String(intNum int64) (int64Str string) { } // Float64转字符串 -// floatNum:float64数字 -// prec:精度位数(不传则默认float数字精度) +// +// floatNum:float64数字 +// prec:精度位数(不传则默认float数字精度) func Float64ToString(floatNum float64, prec ...int) (floatStr string) { if len(prec) > 0 { floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64) @@ -66,8 +71,9 @@ func Float64ToString(floatNum float64, prec ...int) (floatStr string) { } // Float32转字符串 -// floatNum:float32数字 -// prec:精度位数(不传则默认float数字精度) +// +// floatNum:float32数字 +// prec:精度位数(不传则默认float数字精度) func Float32ToString(floatNum float32, prec ...int) (floatStr string) { if len(prec) > 0 { floatStr = strconv.FormatFloat(float64(floatNum), 'f', prec[0], 32) @@ -90,15 +96,6 @@ func BinaryToDecimal(bit string) (num int) { return } -// BytesToString 0 拷贝转换 slice byte 为 string -func BytesToString(b []byte) (s string) { - _bptr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - _sptr := (*reflect.StringHeader)(unsafe.Pointer(&s)) - _sptr.Data = _bptr.Data - _sptr.Len = _bptr.Len - return s -} - // interface to string func AnyToString(in any) (s string) { if in == nil { diff --git a/utils/random.go b/utils/random.go index 6a28462..176bb2b 100644 --- a/utils/random.go +++ b/utils/random.go @@ -2,7 +2,6 @@ package utils import ( "math/rand/v2" - "time" ) // 随机生成字符串 @@ -10,11 +9,10 @@ func RandomString(l int) string { str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" bytes := []byte(str) var result []byte = make([]byte, 0, l) - r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < l; i++ { - result = append(result, bytes[r.Intn(len(bytes))]) + result = append(result, bytes[rand.IntN(len(bytes))]) } - return BytesToString(result) + return string(result) } // 随机生成纯字符串 @@ -22,11 +20,21 @@ func RandomPureString(l int) string { str := "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" bytes := []byte(str) var result []byte = make([]byte, 0, l) - r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < l; i++ { - result = append(result, bytes[r.Intn(len(bytes))]) + result = append(result, bytes[rand.IntN(len(bytes))]) } - return BytesToString(result) + return string(result) +} + +// 随机生成纯大写字符串 +func RandomPureUpString(l int) string { + str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + bytes := []byte(str) + var result []byte = make([]byte, 0, l) + for i := 0; i < l; i++ { + result = append(result, bytes[rand.IntN(len(bytes))]) + } + return string(result) } // 随机生成数字字符串 @@ -34,9 +42,8 @@ func RandomNumber(l int) string { str := "0123456789" bytes := []byte(str) var result []byte - r := rand.New(rand.NewSource(time.Now().UnixNano())) for i := 0; i < l; i++ { - result = append(result, bytes[r.Intn(len(bytes))]) + result = append(result, bytes[rand.IntN(len(bytes))]) } - return BytesToString(result) + return string(result) }