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,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转字符串
// floatNumfloat64数字
// prec精度位数不传则默认float数字精度
//
// floatNumfloat64数字
// 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转字符串
// floatNumfloat32数字
// prec精度位数不传则默认float数字精度
//
// floatNumfloat32数字
// 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 {

View File

@ -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)
}