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 DefaultTrustedProxies() []string
- 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 ResolveClientIP(remote, xff, xri string, trustedProxies []string) 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 DefaultTrustedProxies ¶ added in v0.25.2
func DefaultTrustedProxies() []string
DefaultTrustedProxies 返回默认可信代理 CIDR 列表的副本。
返回副本而非包级切片本身:包级切片一旦被调用方改写(append / 排序 / 改元素), 会静默改变本包 GetClientIP 的判定口径。框架适配层(如 Hertz 侧)用本函数 取同一份默认口径,避免各框架各抄一份内网网段清单后各自漂移。
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。
本函数是 net/http 适配层,算法本体在 ResolveClientIP(非 net/http 框架也可复用)。
func ResolveClientIP ¶ added in v0.25.2
ResolveClientIP 是框架无关的客户端 IP 解析纯函数——HTTP 框架只负责把 「对端地址 + 两个代理头」取出来,采信判定全部在本函数内。
入参:
- remote:对端地址,形态为 "ip:port" 或裸 IP(IPv4 / IPv6);解析不出端口时按原样当 IP 用。
- xff:X-Forwarded-For 头原文,多段以逗号分隔。
- xri:X-Real-IP 头原文。
- trustedProxies:可信代理 CIDR 列表;nil 或空时只认 remote(代理头一律忽略)。
采信规则:
- remote 不在 trustedProxies 内 ⇒ 返回 remote,**忽略一切代理头** (直连方可以随意伪造 XFF,任何代理头都不可信)。
- remote 在 trustedProxies 内 ⇒ 从 XFF **右向左**扫描,跳过可信代理, 返回第一个非可信 IP;XFF 缺失时回退 X-Real-IP;两者都缺失时返回 remote。
- XFF 整条链都是可信代理(如内网互调)⇒ 回退到最左端的原始发起方。
为什么从右向左:XFF 语义是每个代理把上一跳追加到右端,左侧是客户端可任意伪造的; 从左向右扫描会把攻击者注入的伪造值当成客户端 IP(限流 key / 审计 / 访问控制被绕过)。
纯函数形态便于单测覆盖伪造 XFF 场景,也便于非 net/http 框架(Hertz 等) 复用同一份采信口径,避免各仓各写一遍后口径漂移。
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.