243 lines
5.4 KiB
Go
243 lines
5.4 KiB
Go
// Package utils 提供通用工具函数
|
||
// 包括数据类型转换、时间处理、网络工具等
|
||
package utils
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"math"
|
||
"reflect"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
// String2Int 字符串转Int
|
||
// intStr: 数字的字符串
|
||
// 返回: 转换后的整数,转换失败返回0
|
||
func String2Int(intStr string) (intNum int) {
|
||
intNum, err := strconv.Atoi(intStr)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return
|
||
}
|
||
|
||
// String2Int64 字符串转Int64
|
||
// intStr: 数字的字符串
|
||
// 返回: 转换后的64位整数,转换失败返回0
|
||
func String2Int64(intStr string) (int64Num int64) {
|
||
intNum, err := strconv.ParseInt(intStr, 10, 64)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return intNum
|
||
}
|
||
|
||
// String2Float64 字符串转Float64
|
||
// floatStr: 小数点数字的字符串
|
||
// 返回: 转换后的64位浮点数,转换失败返回0
|
||
func String2Float64(floatStr string) (floatNum float64) {
|
||
floatNum, err := strconv.ParseFloat(floatStr, 64)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return
|
||
}
|
||
|
||
// String2Float32 字符串转Float32
|
||
// floatStr: 小数点数字的字符串
|
||
// 返回: 转换后的32位浮点数,转换失败返回0
|
||
func String2Float32(floatStr string) (floatNum float32) {
|
||
floatNum64, err := strconv.ParseFloat(floatStr, 32)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
floatNum = float32(floatNum64)
|
||
return
|
||
}
|
||
|
||
// Int2String Int转字符串
|
||
// intNum: 整数
|
||
// 返回: 转换后的字符串
|
||
func Int2String(intNum int) (intStr string) {
|
||
intStr = strconv.Itoa(intNum)
|
||
return
|
||
}
|
||
|
||
// Int642String Int64转字符串
|
||
// intNum: 64位整数
|
||
// 返回: 转换后的字符串
|
||
func Int642String(intNum int64) (int64Str string) {
|
||
// 10代表10进制
|
||
int64Str = strconv.FormatInt(intNum, 10)
|
||
return
|
||
}
|
||
|
||
// Float64ToString Float64转字符串
|
||
// floatNum: float64数字
|
||
// prec: 精度位数(不传则默认float数字精度)
|
||
// 返回: 转换后的字符串
|
||
func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
|
||
if len(prec) > 0 {
|
||
floatStr = strconv.FormatFloat(floatNum, 'f', prec[0], 64)
|
||
return
|
||
}
|
||
floatStr = strconv.FormatFloat(floatNum, 'f', -1, 64)
|
||
return
|
||
}
|
||
|
||
// Float32ToString Float32转字符串
|
||
// 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)
|
||
return
|
||
}
|
||
floatStr = strconv.FormatFloat(float64(floatNum), 'f', -1, 32)
|
||
return
|
||
}
|
||
|
||
// BinaryToDecimal 二进制转10进制
|
||
// bit: 二进制字符串
|
||
// 返回: 转换后的十进制数
|
||
func BinaryToDecimal(bit string) (num int) {
|
||
fields := strings.Split(bit, "")
|
||
lens := len(fields)
|
||
var tempF float64 = 0
|
||
for i := range lens {
|
||
floatNum := String2Float64(fields[i])
|
||
tempF += floatNum * math.Pow(2, float64(lens-i-1))
|
||
}
|
||
num = int(tempF)
|
||
return
|
||
}
|
||
|
||
// AnyToString 将任意值转为可读的字符串:nil 为空串;标量与 []byte 用 strconv;指针会解引用;其余走 fmt.Sprint。
|
||
func AnyToString(in any) string {
|
||
for in != nil {
|
||
rv := reflect.ValueOf(in)
|
||
if rv.Kind() != reflect.Ptr {
|
||
break
|
||
}
|
||
if rv.IsNil() {
|
||
return ""
|
||
}
|
||
in = rv.Elem().Interface()
|
||
}
|
||
if in == nil {
|
||
return ""
|
||
}
|
||
|
||
switch v := in.(type) {
|
||
case string:
|
||
return v
|
||
case []byte:
|
||
return string(v)
|
||
case bool:
|
||
return strconv.FormatBool(v)
|
||
case int:
|
||
return strconv.Itoa(v)
|
||
case int8:
|
||
return strconv.FormatInt(int64(v), 10)
|
||
case int16:
|
||
return strconv.FormatInt(int64(v), 10)
|
||
case int32:
|
||
return strconv.FormatInt(int64(v), 10)
|
||
case int64:
|
||
return strconv.FormatInt(v, 10)
|
||
case uint:
|
||
return strconv.FormatUint(uint64(v), 10)
|
||
case uint8:
|
||
return strconv.FormatUint(uint64(v), 10)
|
||
case uint16:
|
||
return strconv.FormatUint(uint64(v), 10)
|
||
case uint32:
|
||
return strconv.FormatUint(uint64(v), 10)
|
||
case uint64:
|
||
return strconv.FormatUint(v, 10)
|
||
case float32:
|
||
return strconv.FormatFloat(float64(v), 'f', -1, 32)
|
||
case float64:
|
||
return strconv.FormatFloat(v, 'f', -1, 64)
|
||
case json.Number:
|
||
return string(v)
|
||
default:
|
||
if s, ok := in.(fmt.Stringer); ok {
|
||
return s.String()
|
||
}
|
||
return fmt.Sprint(in)
|
||
}
|
||
}
|
||
|
||
// AnyToInt 将动态类型转为 int(两仓库 internal 中逻辑一致,此处合并分支)。
|
||
func AnyToInt(v any) int {
|
||
switch val := v.(type) {
|
||
case int:
|
||
return val
|
||
case int8:
|
||
return int(val)
|
||
case int16:
|
||
return int(val)
|
||
case int32:
|
||
return int(val)
|
||
case int64:
|
||
return int(val)
|
||
case uint, uint8, uint16, uint32, uint64:
|
||
return int(reflect.ValueOf(val).Uint())
|
||
case float32:
|
||
return int(val)
|
||
case float64:
|
||
return int(val)
|
||
case string:
|
||
i, err := strconv.Atoi(val)
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return i
|
||
default:
|
||
return 0
|
||
}
|
||
}
|
||
|
||
// AnyToFloat64 将动态类型转为 float64(合并 stock 对 int/uint 等分支与 gostock 的 string 分支)。
|
||
func AnyToFloat64(v any) float64 {
|
||
switch val := v.(type) {
|
||
case float64:
|
||
return val
|
||
case float32:
|
||
return float64(val)
|
||
case int:
|
||
return float64(val)
|
||
case int8:
|
||
return float64(val)
|
||
case int16:
|
||
return float64(val)
|
||
case int32:
|
||
return float64(val)
|
||
case int64:
|
||
return float64(val)
|
||
case uint:
|
||
return float64(val)
|
||
case uint8:
|
||
return float64(val)
|
||
case uint16:
|
||
return float64(val)
|
||
case uint32:
|
||
return float64(val)
|
||
case uint64:
|
||
return float64(val)
|
||
case string:
|
||
return String2Float64(val)
|
||
case json.Number:
|
||
f, err := val.Float64()
|
||
if err != nil {
|
||
return 0
|
||
}
|
||
return f
|
||
default:
|
||
return 0
|
||
}
|
||
}
|