ai update.

This commit is contained in:
2025-10-03 19:55:20 +08:00
parent 0401a39a94
commit 464617626b
15 changed files with 374 additions and 378 deletions

View File

@@ -1,3 +1,5 @@
// Package utils 提供通用工具函数
// 包括数据类型转换、时间处理、网络工具等
package utils
import (
@@ -7,8 +9,8 @@ import (
)
// String2Int 字符串转Int
//
// intStr数字的字符串
// intStr: 数字的字符串
// 返回: 转换后的整数转换失败返回0
func String2Int(intStr string) (intNum int) {
intNum, err := strconv.Atoi(intStr)
if err != nil {
@@ -18,20 +20,19 @@ func String2Int(intStr string) (intNum int) {
}
// String2Int64 字符串转Int64
//
// intStr数字的字符串
// intStr: 数字的字符串
// 返回: 转换后的64位整数转换失败返回0
func String2Int64(intStr string) (int64Num int64) {
intNum, err := strconv.Atoi(intStr)
intNum, err := strconv.ParseInt(intStr, 10, 64)
if err != nil {
return 0
}
int64Num = int64(intNum)
return
return intNum
}
// String2Float64 字符串转Float64
//
// floatStr小数点数字的字符串
// floatStr: 小数点数字的字符串
// 返回: 转换后的64位浮点数转换失败返回0
func String2Float64(floatStr string) (floatNum float64) {
floatNum, err := strconv.ParseFloat(floatStr, 64)
if err != nil {
@@ -41,8 +42,8 @@ func String2Float64(floatStr string) (floatNum float64) {
}
// String2Float32 字符串转Float32
//
// floatStr小数点数字的字符串
// floatStr: 小数点数字的字符串
// 返回: 转换后的32位浮点数转换失败返回0
func String2Float32(floatStr string) (floatNum float32) {
floatNum64, err := strconv.ParseFloat(floatStr, 32)
if err != nil {
@@ -52,27 +53,27 @@ func String2Float32(floatStr string) (floatNum float32) {
return
}
// Int转字符串
//
// intNum数字字符串
// Int2String Int转字符串
// intNum: 整数
// 返回: 转换后的字符串
func Int2String(intNum int) (intStr string) {
intStr = strconv.Itoa(intNum)
return
}
// Int64转字符串
//
// intNum数字字符串
// Int642String Int64转字符串
// intNum: 64位整数
// 返回: 转换后的字符串
func Int642String(intNum int64) (int64Str string) {
//10, 代表10进制
// 10代表10进制
int64Str = strconv.FormatInt(intNum, 10)
return
}
// Float64转字符串
//
// floatNumfloat64数字
// prec精度位数不传则默认float数字精度
// 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)
@@ -82,10 +83,10 @@ func Float64ToString(floatNum float64, prec ...int) (floatStr string) {
return
}
// Float32转字符串
//
// floatNumfloat32数字
// prec精度位数不传则默认float数字精度
// 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)
@@ -95,7 +96,9 @@ func Float32ToString(floatNum float32, prec ...int) (floatStr string) {
return
}
// 二进制转10进制
// BinaryToDecimal 二进制转10进制
// bit: 二进制字符串
// 返回: 转换后的十进制数
func BinaryToDecimal(bit string) (num int) {
fields := strings.Split(bit, "")
lens := len(fields)
@@ -108,7 +111,9 @@ func BinaryToDecimal(bit string) (num int) {
return
}
// interface to string
// AnyToString 任意类型转字符串
// in: 输入值
// 返回: 转换后的字符串
func AnyToString(in any) (s string) {
if in == nil {
return ""

View File

@@ -1,3 +1,5 @@
// Package utils 提供通用工具函数
// 包括数据类型转换、时间处理、网络工具等
package utils
import (
@@ -13,6 +15,9 @@ import (
"strings"
)
// IsPublicIP 判断是否为公网IP
// ipString: IP地址字符串
// 返回: 是否为公网IP
func IsPublicIP(ipString string) bool {
ip := net.ParseIP(ipString)
if ip.IsLoopback() || ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() {
@@ -20,11 +25,11 @@ func IsPublicIP(ipString string) bool {
}
if ip4 := ip.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
case ip4[0] == 10: // 10.0.0.0/8
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31: // 172.16.0.0/12
return false
case ip4[0] == 192 && ip4[1] == 168:
case ip4[0] == 192 && ip4[1] == 168: // 192.168.0.0/16
return false
default:
return true
@@ -33,7 +38,8 @@ func IsPublicIP(ipString string) bool {
return false
}
// Get Location IP .
// GetLocationIP 获取本机IP地址
// 返回: 本机IP地址
func GetLocationIP() (localIp string) {
localIp = "127.0.0.1"
// Get all network interfaces

View File

@@ -1,13 +1,23 @@
// Package utils 提供通用工具函数
// 包括时间处理、数据类型转换、网络工具等
package utils
import (
"time"
)
// Time2String 将时间转换为字符串
// layout: 时间格式
// t: 时间对象
// 返回: 格式化的时间字符串
func Time2String(layout string, t time.Time) string {
return t.Format(layout)
}
// String2Time 将字符串转换为时间
// layout: 时间格式
// in: 时间字符串
// 返回: 时间对象
func String2Time(layout, in string) time.Time {
t, _ := time.ParseInLocation(layout, in, time.Local)
return t