Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-23 09:11:20 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-12-24 18:59:29 * @FilePath: \go-toolbox\pkg\types\bound.go * @Description: 边界和范围相关的类型定义 * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-30 00:00:00 * @FilePath: \go-toolbox\pkg\types\convertible.go * @Description: 可转换类型约束定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-01-25 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-13 13:08:33 * @FilePath: \go-toolbox\pkg\types\kind.go * @Description: 类型判断辅助函数 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-13 15:55:18 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-13 15:55:18 * @FilePath: \go-toolbox\internal\kv.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-13 15:55:18 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-13 15:55:18 * @FilePath: \go-toolbox\internal\types\map.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-13 15:55:18 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-13 10:00:55 * @FilePath: \go-toolbox\pkg\types\number.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-05-09 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-09 00:00:00 * @FilePath: \go-toolbox\pkg\types\pointer.go * @Description: 泛型指针辅助工具 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-05-09 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-11 13:17:27 * @FilePath: \apex\go-toolbox\pkg\types\reflect.go * @Description: 反射工具函数
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-13 15:55:18 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-08-12 17:35:16 * @FilePath: \go-toolbox\pkg\types\routine.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-01-05 15:30:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-01-05 11:26:10 * @FilePath: \go-toolbox\pkg\types\slice.go * @Description: 切片通用操作(泛型实现) * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-01-08 13:55:22 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-01-08 13:55:22 * @FilePath: \go-toolbox\pkg\types\time.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-05-13 13:27:53 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-13 13:51:15 * @FilePath: \go-toolbox\pkg\types\type_check.go * @Description: 检查类型兼容性 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
Index ¶
- Variables
- func CheckTypeCompatibility(srcType, dstType reflect.Type) error
- func Chunk[T any](slice []T, size int) [][]T
- func Contains[T comparable](slice []T, value T) bool
- func ContainsAll[T comparable](slice []T, values ...T) bool
- func ContainsAny[T comparable](slice []T, values ...T) bool
- func Deref[T any](p *T) T
- func DerefOrDefault[T any](p *T, defaultValue T) T
- func DerefValue(value interface{}) (interface{}, bool)
- func EnsureStructDefaults(v reflect.Value)
- func ExtractJSONKey(fieldType reflect.StructField) string
- func Filter[T any](slice []T, predicate func(T) bool) []T
- func GetReflectKind(value interface{}) reflect.Kind
- func HasJSONTagOption(fieldType reflect.StructField, options ...string) bool
- func IndexOf[T comparable](slice []T, value T) int
- func IsCEmpty[T comparable](v T) bool
- func IsExportedField(field reflect.StructField) bool
- func IsFloatKind(kind reflect.Kind) bool
- func IsFuncType[T any]() bool
- func IsIntegerKind(kind reflect.Kind) bool
- func IsNil(x interface{}) bool
- func IsNilPtr[T any](p *T) bool
- func IsNonNilPtr[T any](p *T) bool
- func IsNumericKind(kind reflect.Kind) bool
- func IsProtoMessageType(t reflect.Type) bool
- func IsWholeNumber(f float64) bool
- func JSONFieldName(fieldType reflect.StructField) string
- func MapTR[T any, R any](slice []T, mapper func(T) R) []R
- func NewProtoMessage[T proto.Message]() T
- func Ptr[T any](v T) *T
- func Reverse[T any](slice []T) []T
- func ToFloat64OK(value interface{}) (float64, bool)
- func Unique[T comparable](slices ...[]T) []T
- type BoundError
- type BoundType
- type Bounds
- type Complex
- type Convertible
- type Float
- type IntFaceKV
- type IntFaceMap
- type IntFloatKV
- type IntFloatMap
- type IntIntKV
- type IntMap
- type IntUintKV
- type IntUintMap
- type Integer
- type KVMap
- type KeyValueMode
- type Map
- type MinMaxFunc
- type Numerical
- type Ordered
- type RangeMode
- type RangeParser
- type RangeTransformer
- type RangeValidator
- type StrFaceKV
- type StrFaceMap
- type StrFloatKV
- type StrFloatMap
- type StrIntKV
- type StrIntMap
- type StrMap
- type StrStrKV
- type StrUintKV
- type StrUintMap
- type TimeUnit
- type UintFaceKV
- type UintFaceMap
- type UintFloatKV
- type UintFloatMap
- type UintIntKV
- type UintIntMap
- type UintMap
- type UintUintKV
- type Unsigned
Constants ¶
This section is empty.
Variables ¶
var (
ErrTypeMismatchStrict = "type mismatch: cannot assign %s to %s"
)
ProtoMessageType protobuf 消息类型
Functions ¶
func CheckTypeCompatibility ¶ added in v0.12.0
CheckTypeCompatibility 检查类型兼容性
func Contains ¶ added in v0.11.87
func Contains[T comparable](slice []T, value T) bool
Contains 检查值是否在切片中(泛型版本,支持任意可比较类型)
func ContainsAll ¶ added in v0.11.87
func ContainsAll[T comparable](slice []T, values ...T) bool
ContainsAll 检查切片中是否包含所有目标值
func ContainsAny ¶ added in v0.11.87
func ContainsAny[T comparable](slice []T, values ...T) bool
ContainsAny 检查切片中是否包含任意一个目标值
func Deref ¶ added in v0.12.0
func Deref[T any](p *T) T
Deref 解引用指针,返回底层值;如果指针为 nil 则返回类型零值 示例: types.Deref((*int)(nil)) -> 0
func DerefOrDefault ¶ added in v0.12.0
func DerefOrDefault[T any](p *T, defaultValue T) T
DerefOrDefault 解引用指针,返回底层值;如果指针为 nil 则返回默认值 示例: types.DerefOrDefault((*string)(nil), "hello") -> "hello"
func DerefValue ¶ added in v0.12.0
func DerefValue(value interface{}) (interface{}, bool)
DerefValue 解引用指针,返回解引用后的值和是否成功
func EnsureStructDefaults ¶ added in v0.12.0
EnsureStructDefaults 确保结构体字段默认值
func ExtractJSONKey ¶ added in v0.12.0
func ExtractJSONKey(fieldType reflect.StructField) string
ExtractJSONKey 从结构体字段标签中提取 JSON 键名
func GetReflectKind ¶ added in v0.12.0
GetReflectKind 获取反射类型的Kind
func HasJSONTagOption ¶ added in v0.12.0
func HasJSONTagOption(fieldType reflect.StructField, options ...string) bool
HasJSONTagOption 判断结构体字段的 json tag 是否包含指定选项
func IndexOf ¶ added in v0.11.87
func IndexOf[T comparable](slice []T, value T) int
IndexOf 返回值在切片中的索引,不存在返回 -1
func IsExportedField ¶ added in v0.12.0
func IsExportedField(field reflect.StructField) bool
IsExportedField 判断结构体字段是否可按 JSON 规则参与导出处理
func IsIntegerKind ¶ added in v0.12.0
IsIntegerKind 判断是否为整数类型
func IsNumericKind ¶ added in v0.12.0
IsNumericKind 判断是否为数值类型
func IsProtoMessageType ¶ added in v0.12.0
IsProtoMessageType 判断类型是否实现 proto.Message 接口
func JSONFieldName ¶ added in v0.12.0
func JSONFieldName(fieldType reflect.StructField) string
JSONFieldName 获取结构体字段的 JSON 字段名;没有显式名称时返回 Go 字段名
func NewProtoMessage ¶ added in v0.12.0
NewProtoMessage 创建 protobuf 消息实例
func Ptr ¶ added in v0.12.0
func Ptr[T any](v T) *T
Ptr 创建任意类型的指针(泛型版本,比 validator 包中 IntPtr/BoolPtr 等更通用) 示例: p := types.Ptr(42) -> *int
func ToFloat64OK ¶ added in v0.12.0
ToFloat64OK 尝试将值转换为 float64 类型,返回转换结果和是否成功
func Unique ¶ added in v0.11.87
func Unique[T comparable](slices ...[]T) []T
Unique 去重切片(保持原始顺序),支持合并多个切片后去重 性能优化: - 使用 map 快速查重,每次查找 O(1),总体时间复杂度 O(n) - 预分配合理容量减少内存分配 - 单次遍历完成去重
Types ¶
type BoundError ¶ added in v0.11.87
type BoundError int
BoundError 边界错误类型
const ( BoundErrorNone BoundError = iota // 无错误 BoundErrorBelowMin // 低于最小值 BoundErrorAboveMax // 超过最大值 BoundErrorInvalidRange // 无效范围(min > max) BoundErrorZeroStep // 步长为零 BoundErrorNegative // 负数错误 )
type Bounds ¶ added in v0.11.87
type Bounds[T Numerical] struct { Min T // 最小值 Max T // 最大值 Names map[string]T // 名称映射(如 "jan"->1, "mon"->1) }
Bounds 定义字段的取值范围(泛型)
type Complex ¶
type Complex interface {
~complex64 | ~complex128
}
Complex 是一个约束,允许任何复数数值类型。 如果未来的 Go 版本添加了新的预定义复数数值类型, 这个约束将会被修改以包含它们。
type Convertible ¶ added in v0.11.87
Convertible 是一个约束,包含所有可以通过 convert.MustConvertTo 转换的类型 包括:基础类型(string, bool)、所有数字类型、字节切片、字典和切片
type KVMap ¶
type KVMap interface {
StrStrKV | StrIntKV | StrUintKV |
IntIntKV | IntUintKV | UintIntKV | UintUintKV |
StrFloatKV | IntFloatKV | UintFloatKV |
StrFaceKV | IntFaceKV | UintFaceKV
}
组合接口,包含所有的键值类型
type KeyValueMode ¶
type KeyValueMode[K comparable, V any] struct { Key K Value V }
KeyValueMode 定义一个通用的键值对结构
type Map ¶
type Map interface {
StrMap | StrIntMap | StrUintMap |
IntMap | IntUintMap | UintIntMap | UintMap |
StrFloatMap | IntFloatMap | UintFloatMap |
StrFaceMap | IntFaceMap | UintFaceMap
}
组合接口,包含所有的映射类型
type MinMaxFunc ¶
type MinMaxFunc[T any] func(a, b T) T
MinMaxFunc 是用于计算最小值或最大值的函数类型,接收两个interface{}类型的参数,返回一个interface{}类型的结果。
type Numerical ¶
Numerical 是一个接口,表示一系列数值类型,包括有符号和无符号的整数以及浮点数。 使用 ~ 支持底层类型相同的类型别名(如 time.Duration 是 ~int64)
type RangeParser ¶ added in v0.11.87
RangeParser 范围解析器类型(函数类型)
type RangeTransformer ¶ added in v0.11.87
RangeTransformer 范围转换器类型(函数类型)
type RangeValidator ¶ added in v0.11.87
type RangeValidator[T Numerical] func(value T, bounds Bounds[T]) BoundError
RangeValidator 范围验证器类型(函数类型)
type StrFaceMap ¶
type StrFaceMap interface {
map[string]interface{}
}
StrFaceMap 表示一个以字符串为键,任意类型为值的映射
type StrFloatMap ¶
StrFloatMap 表示一个以字符串为键,浮点数为值的映射
type UintFaceKV ¶
type UintFaceKV KeyValueMode[uint, interface{}]
UintFaceKV 表示一个以无符号整数为键,任意类型为值的键值对
type UintFaceMap ¶
type UintFaceMap interface {
map[uint]interface{}
}
UintFaceMap 表示一个以无符号整数为键,任意类型为值的映射
type UintFloatMap ¶
UintFloatMap 表示一个以无符号整数为键,浮点数为值的映射