conv

package
v1.8.7 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilData 表示输入数据为空
	// ErrNilData indicates input data is nil
	ErrNilData = errors.New("conv: data cannot be nil")

	// ErrNilTarget 表示目标对象为空
	// ErrNilTarget indicates target object is nil
	ErrNilTarget = errors.New("conv: target cannot be nil")

	// ErrTargetNotPointer 表示目标对象不是有效指针类型
	// ErrTargetNotPointer indicates target is not a valid pointer type
	ErrTargetNotPointer = errors.New("conv: target must be non-nil pointer")
)

包级别错误定义,避免重复创建错误对象 / Package-level error definitions to avoid recreating error objects

Functions

func Bool

func Bool(any interface{}) bool

Bool converts `any` to bool. 将 `any` 转换为 bool。

func Byte

func Byte(any interface{}) byte

Byte converts any type to byte. Maintains original value for byte types, converts to uint8 for other types (supports numeric/string/bool compatibility) 将任意类型转换为byte类型。保留byte类型的原始值,其他类型转换为uint8(支持数字/字符串/布尔型兼容)

func Bytes

func Bytes(any interface{}) []byte

Bytes converts any type to []byte with optimized memory allocation. Special handling for basic types, JSON serialization for complex types 将任意类型转换为[]byte(优化内存分配),基础类型特殊处理,复杂类型使用JSON序列化

func Float32

func Float32(any interface{}) float32

Float32 converts any type to float32. Note that this conversion may lose precision for large integers or high-precision decimals. 将任意类型转换为 float32。注意,转换大整数或高精度小数时可能丢失精度。

func Float64

func Float64(any interface{}) float64

Float64 converts any type to float64. Provides higher precision than Float32 but still has limitations with very large numbers. 将任意类型转换为 float64。相比 Float32 精度更高,但对极大数值仍有限制。

func GobDecoder added in v1.7.2

func GobDecoder(data []byte, target any) error

GobDecoder 使用gob解码数据(增强安全版本) 特性: 1. 严格参数类型校验 2. 详细的错误类型返回 3. 自动处理缓冲区

GobDecoder decodes data using gob (enhanced safety version) Features: 1. Strict parameter type validation 2. Detailed error type returns 3. Automatic buffer handling

func GobEncoder added in v1.7.2

func GobEncoder(data any) ([]byte, error)

GobEncoder 使用gob编码数据(高性能版本) 特性: 1. 使用内存池减少内存分配 2. 严格参数校验 3. 自动处理缓冲区生命周期

GobEncoder encodes data using gob (high-performance version) Features: 1. Uses memory pool to reduce allocations 2. Strict parameter validation 3. Automatic buffer lifecycle management

func Int

func Int(any interface{}) int

Int converts "any" to int. Rules: <nil> => 0, bool => 0/1, numeric => value truncation, []byte => big-endian conversion, string => base10 parsing, others => 0 转换规则:<nil>转0,布尔转0/1,数字类型截断转换,字节切片按大端序转,字符串十进制解析,其他类型返回0

func Int16

func Int16(any interface{}) (v int16)

Int16 converts "any" to int16 with overflow checking. 带溢出检查的int16转换

func Int32

func Int32(any interface{}) (v int32)

Int32 converts "any" to int32 with overflow checking. 带溢出检查的int32转换

func Int64

func Int64(any interface{}) int64

Int64 converts "any" to int64 with value clamping and safe parsing. Supported types: all numeric types, bool, []byte, string 安全转换函数,支持所有数字类型、布尔、字节切片和字符串

func Int8

func Int8(any interface{}) (v int8)

Int8 converts "any" to int8 with overflow checking. 带溢出检查的int8转换,处理方式与Int类似

func Interfaces

func Interfaces(any interface{}) []interface{}

Interfaces 将 `any` 转换为 []interface{}。 Converts `any` to []interface{}.

func Ints

func Ints(any interface{}) []int

Ints 将 `any` 转换为 []int。 Converts `any` to []int.

func Map

func Map(any interface{}) (data map[string]interface{})

Map converts any type to map[string]interface{} with panic handling. Supported types: JSON bytes/string, map, struct 将任意类型转换为map[string]interface{},包含panic处理 支持类型:JSON字节/字符串、map、结构体

func MapInt

func MapInt(any interface{}) map[int]interface{}

MapInt converts any type to map[int]interface{} with key validation. Requires JSON keys to be numeric strings (e.g., "123") 将任意类型转换为map[int]interface{},包含键值验证 要求JSON键为数字字符串(如"123")

func MapString

func MapString(any interface{}) map[string]string

MapString converts any type to map[string]string with type compatibility. Supports all JSON value types converted to string via fmt.Sprintf 将任意类型转换为map[string]string,增强类型兼容性 支持通过fmt.Sprintf将所有JSON值类型转为字符串

func Maps added in v1.4.7

func Maps(any interface{}) (data []map[string]interface{})

Maps converts any type to []map[string]interface{} with panic handling. Auto-converts JSON strings/bytes to slice 将任意类型转换为[]map[string]interface{},包含panic处理 自动转换JSON字符串/字节为切片

func Rune

func Rune(any interface{}) rune

Rune converts any type to rune. Maintains original value for rune types, converts to int32 for other types (supports numeric/string/bool compatibility) 将任意类型转换为rune类型。保留rune类型的原始值,其他类型转换为int32(支持数字/字符串/布尔型兼容)

func Runes

func Runes(any interface{}) []rune

Runes converts any type to []rune. Direct conversion for []rune types, converts to string first for other types 将任意类型转换为[]rune类型。直接转换[]rune类型,其他类型先转换为字符串

func String

func String(any interface{}) string

String converts any type to string with optimized type handling. Specialized conversion for basic types, JSON serialization for others 将任意类型转换为字符串(优化类型处理),基础类型特殊转换,其他类型使用JSON序列化

func Strings

func Strings(any interface{}) []string

Strings 将 `any` 转换为 []string。 Converts `any` to []string.

func ToJSON added in v1.7.2

func ToJSON(data any) ([]byte, error)

ToJSON serializes the provided data into JSON byte format. 将提供的数据序列化为 JSON 字节格式。 data: The source data to be serialized into JSON.

data: 需要序列化为 JSON 的源数据。

Returns the JSON byte slice or an error if serialization fails. 返回 JSON 字节切片,如果序列化失败,则返回错误。

func ToStruct added in v1.7.2

func ToStruct(data any, model any) error

ToStruct converts data from any type to the provided model using JSON serialization and deserialization. 使用 JSON 序列化和反序列化将数据从任何类型转换为提供的模型。 data: The source data that will be converted. Can be a struct, map, or slice.

data: 需要转换的源数据,可以是结构体、映射或切片。

model: The target model that will hold the converted data.

model: 目标模型,将存储转换后的数据。

Returns an error if the conversion fails. 如果转换失败,返回错误。

func Uint

func Uint(any interface{}) uint

Uint converts "any" to uint with overflow checking. 带溢出检查的uint转换

func Uint16

func Uint16(any interface{}) uint16

Uint16 converts "any" to uint16 with overflow checking. 带溢出检查的uint16转换

func Uint32

func Uint32(any interface{}) uint32

Uint32 converts "any" to uint32 with overflow checking. 带溢出检查的uint32转换

func Uint64

func Uint64(any interface{}) uint64

Uint64 converts "any" to uint64 with value clamping and safe parsing. 安全转换函数,支持所有数字类型、布尔、字节切片和字符串

func Uint8

func Uint8(any interface{}) uint8

Uint8 converts "any" to uint8 with overflow checking. 带溢出检查的uint8转换

func UnmarshalJSON added in v1.7.2

func UnmarshalJSON(data []byte, model any) error

UnmarshalJSON deserializes JSON byte data into the provided model. 使用 JSON 反序列化将字节数据转化为指定的模型。 data: The JSON byte data to be converted into the model.

data: 需要转换为模型的 JSON 字节数据。

model: The target model to store the deserialized data.

model: 目标模型,用于存储反序列化后的数据。

Returns an error if deserialization fails. 如果反序列化失败,返回错误。

Types

This section is empty.

Jump to

Keyboard shortcuts

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