Documentation
¶
Overview ¶
Package convert 提供常用类型转换工具函数。
所有转换函数都支持默认值,转换失败时返回默认值而非 error。
convert.Str2Int64("123", 0) // 123
convert.Str2Int64("abc", 0) // 0 (转换失败返回默认值)
convert.Str2Float64("3.14", 0) // 3.14
convert.Bool2Str(true) // "true"
Index ¶
- func AnyToBool(v any) bool
- func AnyToFloat32(v any) float32
- func AnyToFloat64(v any) float64
- func AnyToInt(v any) int
- func AnyToInt64(v any) int64
- func AnyToStr(v any) string
- func AnyToUint64(v any) uint64
- func AppendStrings(a, b []string) []string
- func Bool2Str(b bool) string
- func BoolToBytes(b bool) []byte
- func ByteToBytes(b byte) []byte
- func BytesToFloat32(data []byte) float32
- func BytesToFloat64(data []byte) float64
- func BytesToInt(data []byte) int
- func BytesToInt64(data []byte) int64
- func BytesToString(data []byte) string
- func Convert(data any, target any) error
- func ConvertStruct(src, dst any) error
- func DeepCopy(value any) any
- func Float32ToStr(f float32, prec int) string
- func Float64ToBytes(f float64, prec int) []byte
- func Float64ToStr(f float64, prec int) string
- func GetClientIP(r *http.Request) string
- func GetClientIPWithProxies(r *http.Request, trustedProxies []string) string
- func Int32ToStr(i int32) string
- func Int64ToBytes(i int64) []byte
- func Int64ToStr(i int64) string
- func IntToBytes(i int) []byte
- func IntToStr(i int) string
- func Str2Bool(s string, def bool) bool
- func Str2Float32(s string, def float32) float32
- func Str2Float64(s string, def float64) float64
- func Str2Int(s string, def int) int
- func Str2Int32(s string, def int32) int32
- func Str2Int64(s string, def int64) int64
- func Str2Uint(s string, def uint) uint
- func Str2Uint64(s string, def uint64) uint64
- func StringToBytes(s string) []byte
- func StructToMap(v any) map[string]any
- func Uint64ToStr(u uint64) string
- func UintToStr(u uint) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyToFloat64 ¶
AnyToFloat64 任意类型转 float64,无法转换返回 0。 支持 bool(true=1, false=0)、string/[]byte(按浮点解析)、所有数值类型。
func AnyToInt64 ¶
AnyToInt64 任意类型转 int64,无法转换返回 0。 支持 bool(true=1, false=0)、string/[]byte(按十进制解析)、所有数值类型。
func AnyToUint64 ¶
AnyToUint64 任意类型转 uint64,无法转换返回 0。 注意:传入负数会按位重新解释为大正数(uint 语义),调用方应自行确保非负。
func ConvertStruct ¶
ConvertStruct 将 src 结构体的同名字段映射到 dst。 dst 必须是指针。通过反射匹配字段名,相同名称且类型兼容的字段会被复制。
type Src struct { Name string; Age int }
type Dst struct { Name string; Age int }
var d Dst
ConvertStruct(Src{Name: "test", Age: 18}, &d)
func DeepCopy ¶
DeepCopy 对 JSON 动态数据(map[string]any / []any 递归嵌套)进行深拷贝。 仅处理 any 承载的动态容器;强类型容器(如 []map[string]any、 map[string][]string)与其他类型原样返回(浅拷贝),调用方自行注意。
func Float32ToStr ¶
Float32ToStr float32 转字符串,prec 为小数位数(-1 为最短表示)。
func Float64ToBytes ¶
Float64ToBytes float64 转字节切片,prec 为小数位数。
func Float64ToStr ¶
Float64ToStr float64 转字符串,prec 为小数位数(-1 为最短表示)。
func GetClientIP ¶
GetClientIP 从 HTTP 请求中提取真实客户端 IP。 优先级:X-Forwarded-For(仅当请求来自可信代理时) > X-Real-IP > RemoteAddr。
安全说明:默认只信任内网代理(10/8, 172.16/12, 192.168/16, 127/8)。 如果部署在公网代理后面(如 Cloudflare),需要通过 GetClientIPWithProxies 指定可信代理列表。
func GetClientIPWithProxies ¶
GetClientIPWithProxies 从 HTTP 请求中提取真实客户端 IP,指定可信代理 CIDR 列表。 只有当 RemoteAddr 属于可信代理时,才信任 X-Forwarded-For 和 X-Real-IP 头。 trustedProxies 为 nil 或空时,只使用 RemoteAddr。
func Str2Float32 ¶
Str2Float32 字符串转 float32,失败返回默认值。
func Str2Float64 ¶
Str2Float64 字符串转 float64,失败返回默认值。
func Str2Uint64 ¶
Str2Uint64 字符串转 uint64,失败返回默认值。 负数字符串会解析失败并返回默认值。
func StructToMap ¶
StructToMap 将结构体转为 map[string]any。 通过 json tag 获取 key 名,无 json tag 则用字段名。
Types ¶
This section is empty.