update go 1.22

This commit is contained in:
david 2024-02-21 20:58:31 +08:00
parent a357fa225b
commit acaaa8d6ed
2 changed files with 35 additions and 31 deletions

View File

@ -2,13 +2,12 @@ package utils
import ( import (
"math" "math"
"reflect"
"strconv" "strconv"
"strings" "strings"
"unsafe"
) )
// 字符串转Int // 字符串转Int
//
// intStr数字的字符串 // intStr数字的字符串
func String2Int(intStr string) (intNum int) { func String2Int(intStr string) (intNum int) {
intNum, _ = strconv.Atoi(intStr) intNum, _ = strconv.Atoi(intStr)
@ -16,6 +15,7 @@ func String2Int(intStr string) (intNum int) {
} }
// 字符串转Int64 // 字符串转Int64
//
// intStr数字的字符串 // intStr数字的字符串
func String2Int64(intStr string) (int64Num int64) { func String2Int64(intStr string) (int64Num int64) {
intNum, _ := strconv.Atoi(intStr) intNum, _ := strconv.Atoi(intStr)
@ -24,6 +24,7 @@ func String2Int64(intStr string) (int64Num int64) {
} }
// 字符串转Float64 // 字符串转Float64
//
// floatStr小数点数字的字符串 // floatStr小数点数字的字符串
func String2Float64(floatStr string) (floatNum float64) { func String2Float64(floatStr string) (floatNum float64) {
floatNum, _ = strconv.ParseFloat(floatStr, 64) floatNum, _ = strconv.ParseFloat(floatStr, 64)
@ -31,6 +32,7 @@ func String2Float64(floatStr string) (floatNum float64) {
} }
// 字符串转Float32 // 字符串转Float32
//
// floatStr小数点数字的字符串 // floatStr小数点数字的字符串
func String2Float32(floatStr string) (floatNum float32) { func String2Float32(floatStr string) (floatNum float32) {
floatNum64, _ := strconv.ParseFloat(floatStr, 32) floatNum64, _ := strconv.ParseFloat(floatStr, 32)
@ -39,6 +41,7 @@ func String2Float32(floatStr string) (floatNum float32) {
} }
// Int转字符串 // Int转字符串
//
// intNum数字字符串 // intNum数字字符串
func Int2String(intNum int) (intStr string) { func Int2String(intNum int) (intStr string) {
intStr = strconv.Itoa(intNum) intStr = strconv.Itoa(intNum)
@ -46,6 +49,7 @@ func Int2String(intNum int) (intStr string) {
} }
// Int64转字符串 // Int64转字符串
//
// intNum数字字符串 // intNum数字字符串
func Int642String(intNum int64) (int64Str string) { func Int642String(intNum int64) (int64Str string) {
//10, 代表10进制 //10, 代表10进制
@ -54,6 +58,7 @@ func Int642String(intNum int64) (int64Str string) {
} }
// Float64转字符串 // Float64转字符串
//
// floatNumfloat64数字 // floatNumfloat64数字
// prec精度位数不传则默认float数字精度 // prec精度位数不传则默认float数字精度
func Float64ToString(floatNum float64, prec ...int) (floatStr string) { func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
@ -66,6 +71,7 @@ func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
} }
// Float32转字符串 // Float32转字符串
//
// floatNumfloat32数字 // floatNumfloat32数字
// prec精度位数不传则默认float数字精度 // prec精度位数不传则默认float数字精度
func Float32ToString(floatNum float32, prec ...int) (floatStr string) { func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
@ -90,15 +96,6 @@ func BinaryToDecimal(bit string) (num int) {
return 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 // interface to string
func AnyToString(in any) (s string) { func AnyToString(in any) (s string) {
if in == nil { if in == nil {

View File

@ -2,7 +2,6 @@ package utils
import ( import (
"math/rand/v2" "math/rand/v2"
"time"
) )
// 随机生成字符串 // 随机生成字符串
@ -10,11 +9,10 @@ func RandomString(l int) string {
str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" str := "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
bytes := []byte(str) bytes := []byte(str)
var result []byte = make([]byte, 0, l) var result []byte = make([]byte, 0, l)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ { 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" str := "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
bytes := []byte(str) bytes := []byte(str)
var result []byte = make([]byte, 0, l) var result []byte = make([]byte, 0, l)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ { 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" str := "0123456789"
bytes := []byte(str) bytes := []byte(str)
var result []byte var result []byte
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ { 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)
} }