Help us shape the future of Go! Take the 2026 Go Developer Survey

convert

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyToBool

func AnyToBool(v any) bool

AnyToBool 任意类型转 bool,无法转换返回 false。 数值类型非零为 true;字符串按 strconv.ParseBool 解析。

func AnyToFloat32

func AnyToFloat32(v any) float32

AnyToFloat32 任意类型转 float32,无法转换返回 0。

func AnyToFloat64

func AnyToFloat64(v any) float64

AnyToFloat64 任意类型转 float64,无法转换返回 0。 支持 bool(true=1, false=0)、string/[]byte(按浮点解析)、所有数值类型。

func AnyToInt

func AnyToInt(v any) int

AnyToInt 任意类型转 int,无法转换返回 0。

func AnyToInt64

func AnyToInt64(v any) int64

AnyToInt64 任意类型转 int64,无法转换返回 0。 支持 bool(true=1, false=0)、string/[]byte(按十进制解析)、所有数值类型。

func AnyToStr

func AnyToStr(v any) string

AnyToStr 任意类型转字符串。

func AnyToUint64

func AnyToUint64(v any) uint64

AnyToUint64 任意类型转 uint64,无法转换返回 0。 注意:传入负数会按位重新解释为大正数(uint 语义),调用方应自行确保非负。

func AppendStrings

func AppendStrings(a, b []string) []string

AppendStrings 将 b 切片追加到 a 切片。

func Bool2Str

func Bool2Str(b bool) string

Bool2Str bool 转字符串。

func BoolToBytes

func BoolToBytes(b bool) []byte

BoolToBytes bool 转字节切片。

func ByteToBytes

func ByteToBytes(b byte) []byte

ByteToBytes 单个字节转字节切片。

func BytesToFloat32

func BytesToFloat32(data []byte) float32

BytesToFloat32 字节切片转 float32,失败返回 0。

func BytesToFloat64

func BytesToFloat64(data []byte) float64

BytesToFloat64 字节切片转 float64,失败返回 0。

func BytesToInt

func BytesToInt(data []byte) int

BytesToInt 字节切片转 int,失败返回 0。

func BytesToInt64

func BytesToInt64(data []byte) int64

BytesToInt64 字节切片转 int64,失败返回 0。

func BytesToString

func BytesToString(data []byte) string

BytesToString 字节切片转字符串。

func Convert

func Convert(data any, target any) error

Convert 通过 JSON 序列化/反序列化将 data 转换到 target。 注意:此方法会丢失精度,不适用于需要精确转换的场景。

func ConvertStruct

func ConvertStruct(src, dst any) error

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

func DeepCopy(value any) any

DeepCopy 对 JSON 动态数据(map[string]any / []any 递归嵌套)进行深拷贝。 仅处理 any 承载的动态容器;强类型容器(如 []map[string]any、 map[string][]string)与其他类型原样返回(浅拷贝),调用方自行注意。

func Float32ToStr

func Float32ToStr(f float32, prec int) string

Float32ToStr float32 转字符串,prec 为小数位数(-1 为最短表示)。

func Float64ToBytes

func Float64ToBytes(f float64, prec int) []byte

Float64ToBytes float64 转字节切片,prec 为小数位数。

func Float64ToStr

func Float64ToStr(f float64, prec int) string

Float64ToStr float64 转字符串,prec 为小数位数(-1 为最短表示)。

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP 从 HTTP 请求中提取真实客户端 IP。 优先级:X-Forwarded-For(仅当请求来自可信代理时) > X-Real-IP > RemoteAddr。

安全说明:默认只信任内网代理(10/8, 172.16/12, 192.168/16, 127/8)。 如果部署在公网代理后面(如 Cloudflare),需要通过 GetClientIPWithProxies 指定可信代理列表。

func GetClientIPWithProxies

func GetClientIPWithProxies(r *http.Request, trustedProxies []string) string

GetClientIPWithProxies 从 HTTP 请求中提取真实客户端 IP,指定可信代理 CIDR 列表。 只有当 RemoteAddr 属于可信代理时,才信任 X-Forwarded-For 和 X-Real-IP 头。 trustedProxies 为 nil 或空时,只使用 RemoteAddr。

func Int32ToStr

func Int32ToStr(i int32) string

Int32ToStr int32 转字符串。

func Int64ToBytes

func Int64ToBytes(i int64) []byte

Int64ToBytes int64 转字节切片(十进制文本)。

func Int64ToStr

func Int64ToStr(i int64) string

Int64ToStr int64 转字符串。

func IntToBytes

func IntToBytes(i int) []byte

IntToBytes int 转字节切片(十进制文本)。

func IntToStr

func IntToStr(i int) string

IntToStr int 转字符串。

func Str2Bool

func Str2Bool(s string, def bool) bool

Str2Bool 字符串转 bool,失败返回默认值。 接受 "1", "t", "T", "TRUE", "true", "True" 为 true。

func Str2Float32

func Str2Float32(s string, def float32) float32

Str2Float32 字符串转 float32,失败返回默认值。

func Str2Float64

func Str2Float64(s string, def float64) float64

Str2Float64 字符串转 float64,失败返回默认值。

func Str2Int

func Str2Int(s string, def int) int

Str2Int 字符串转 int,失败返回默认值。

func Str2Int32

func Str2Int32(s string, def int32) int32

Str2Int32 字符串转 int32,失败返回默认值。

func Str2Int64

func Str2Int64(s string, def int64) int64

Str2Int64 字符串转 int64,失败返回默认值。

func Str2Uint

func Str2Uint(s string, def uint) uint

Str2Uint 字符串转 uint,失败返回默认值。 负数字符串会解析失败并返回默认值。

func Str2Uint64

func Str2Uint64(s string, def uint64) uint64

Str2Uint64 字符串转 uint64,失败返回默认值。 负数字符串会解析失败并返回默认值。

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes 字符串转字节切片。

func StructToMap

func StructToMap(v any) map[string]any

StructToMap 将结构体转为 map[string]any。 通过 json tag 获取 key 名,无 json tag 则用字段名。

func Uint64ToStr

func Uint64ToStr(u uint64) string

Uint64ToStr uint64 转字符串。

func UintToStr

func UintToStr(u uint) string

UintToStr uint 转字符串。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL