types

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: Apache-2.0 Imports: 5 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeMismatchStrict = "type mismatch: cannot assign %s to %s"
)
View Source
var ProtoMessageType = reflect.TypeOf((*proto.Message)(nil)).Elem()

ProtoMessageType protobuf 消息类型

Functions

func CheckTypeCompatibility added in v0.12.0

func CheckTypeCompatibility(srcType, dstType reflect.Type) error

CheckTypeCompatibility 检查类型兼容性

func Chunk added in v0.11.87

func Chunk[T any](slice []T, size int) [][]T

Chunk 将切片分块

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

func EnsureStructDefaults(v reflect.Value)

EnsureStructDefaults 确保结构体字段默认值

func ExtractJSONKey added in v0.12.0

func ExtractJSONKey(fieldType reflect.StructField) string

ExtractJSONKey 从结构体字段标签中提取 JSON 键名

func Filter added in v0.11.87

func Filter[T any](slice []T, predicate func(T) bool) []T

Filter 过滤切片,返回满足条件的元素

func GetReflectKind added in v0.12.0

func GetReflectKind(value interface{}) reflect.Kind

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 IsCEmpty added in v0.12.0

func IsCEmpty[T comparable](v T) bool

IsCEmpty 判断是否为空

func IsExportedField added in v0.12.0

func IsExportedField(field reflect.StructField) bool

IsExportedField 判断结构体字段是否可按 JSON 规则参与导出处理

func IsFloatKind added in v0.12.0

func IsFloatKind(kind reflect.Kind) bool

IsFloatKind 判断是否为浮点数类型

func IsFuncType added in v0.12.0

func IsFuncType[T any]() bool

IsFuncType 判断是否为函数类型

func IsIntegerKind added in v0.12.0

func IsIntegerKind(kind reflect.Kind) bool

IsIntegerKind 判断是否为整数类型

func IsNil added in v0.12.0

func IsNil(x interface{}) bool

IsNil 判断是否为 nil

func IsNilPtr added in v0.12.0

func IsNilPtr[T any](p *T) bool

IsNilPtr 检查指针是否为 nil

func IsNonNilPtr added in v0.12.0

func IsNonNilPtr[T any](p *T) bool

IsNonNilPtr 检查指针是否非 nil

func IsNumericKind added in v0.12.0

func IsNumericKind(kind reflect.Kind) bool

IsNumericKind 判断是否为数值类型

func IsProtoMessageType added in v0.12.0

func IsProtoMessageType(t reflect.Type) bool

IsProtoMessageType 判断类型是否实现 proto.Message 接口

func IsWholeNumber added in v0.12.0

func IsWholeNumber(f float64) bool

IsWholeNumber 判断是否为整数

func JSONFieldName added in v0.12.0

func JSONFieldName(fieldType reflect.StructField) string

JSONFieldName 获取结构体字段的 JSON 字段名;没有显式名称时返回 Go 字段名

func MapTR added in v0.11.87

func MapTR[T any, R any](slice []T, mapper func(T) R) []R

MapTR 映射切片,将每个元素转换为另一种类型

func NewProtoMessage added in v0.12.0

func NewProtoMessage[T proto.Message]() T

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 Reverse added in v0.11.87

func Reverse[T any](slice []T) []T

Reverse 反转切片(返回新切片,不修改原切片)

func ToFloat64OK added in v0.12.0

func ToFloat64OK(value interface{}) (float64, bool)

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 BoundType added in v0.11.87

type BoundType int

BoundType 边界类型枚举

const (
	BoundClosed         BoundType = iota // 闭区间 [min, max]
	BoundOpen                            // 开区间 (min, max)
	BoundLeftOpen                        // 左开右闭 (min, max]
	BoundRightOpen                       // 左闭右开 [min, max)
	BoundLeftUnbounded                   // 左无界 (-∞, max]
	BoundRightUnbounded                  // 右无界 [min, +∞)
	BoundUnbounded                       // 无界 (-∞, +∞)
)

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

type Convertible interface {
	~string | ~bool |
		Numerical |
		~[]byte |
		~map[string]any |
		~[]any
}

Convertible 是一个约束,包含所有可以通过 convert.MustConvertTo 转换的类型 包括:基础类型(string, bool)、所有数字类型、字节切片、字典和切片

type Float

type Float interface {
	~float32 | ~float64
}

Float 是一个约束,允许任何浮点数类型。 如果未来的 Go 版本添加了新的预定义浮点数类型, 这个约束将会被修改以包含它们。

type IntFaceKV

type IntFaceKV KeyValueMode[int, interface{}]

IntFaceKV 表示一个以整数为键,任意类型为值的键值对

type IntFaceMap

type IntFaceMap interface {
	map[int]interface{}
}

IntFaceMap 表示一个以整数为键,任意类型为值的映射

type IntFloatKV

type IntFloatKV KeyValueMode[int, float64]

IntFloatKV 表示一个以整数为键,浮点数为值的键值对

type IntFloatMap

type IntFloatMap interface {
	map[int]float64
}

IntFloatMap 表示一个以整数为键,浮点数为值的映射

type IntIntKV

type IntIntKV KeyValueMode[int, int]

IntIntKV 表示一个以整数为键,整数为值的键值对

type IntMap

type IntMap interface {
	map[int]int
}

IntMap 表示一个以整数为键,整数为值的映射

type IntUintKV

type IntUintKV KeyValueMode[int, uint]

IntUintKV 表示一个以整数为键,无符号整数为值的键值对

type IntUintMap

type IntUintMap interface {
	map[int]uint
}

IntUintMap 表示一个以整数为键,无符号整数为值的映射

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Integer

type KVMap

组合接口,包含所有的键值类型

type KeyValueMode

type KeyValueMode[K comparable, V any] struct {
	Key   K
	Value V
}

KeyValueMode 定义一个通用的键值对结构

type Map

组合接口,包含所有的映射类型

type MinMaxFunc

type MinMaxFunc[T any] func(a, b T) T

MinMaxFunc 是用于计算最小值或最大值的函数类型,接收两个interface{}类型的参数,返回一个interface{}类型的结果。

type Numerical

type Numerical interface {
	Integer | Unsigned | Float
}

Numerical 是一个接口,表示一系列数值类型,包括有符号和无符号的整数以及浮点数。 使用 ~ 支持底层类型相同的类型别名(如 time.Duration 是 ~int64)

type Ordered

type Ordered interface {
	Integer | Unsigned | Float | ~string
}

Ordered 是一个约束,允许任何有序类型:任何支持操作符 < <= >= > 的类型。 如果未来的 Go 版本添加了新的有序类型, 这个约束将会被修改以包含它们。

type RangeMode added in v0.11.87

type RangeMode int

RangeMode 范围解析模式

const (
	RangeModeNormal   RangeMode = iota // 普通模式:精确匹配
	RangeModeWildcard                  // 通配符模式:支持 * 和 ?
	RangeModeStep                      // 步长模式:支持 /step
	RangeModeList                      // 列表模式:支持逗号分隔
)

type RangeParser added in v0.11.87

type RangeParser[T Numerical] func(expr string, bounds Bounds[T]) (T, error)

RangeParser 范围解析器类型(函数类型)

type RangeTransformer added in v0.11.87

type RangeTransformer[T Numerical, R any] func(value T, bounds Bounds[T]) (R, error)

RangeTransformer 范围转换器类型(函数类型)

type RangeValidator added in v0.11.87

type RangeValidator[T Numerical] func(value T, bounds Bounds[T]) BoundError

RangeValidator 范围验证器类型(函数类型)

type StrFaceKV

type StrFaceKV KeyValueMode[string, interface{}]

StrFaceKV 表示一个以字符串为键,任意类型为值的键值对

type StrFaceMap

type StrFaceMap interface {
	map[string]interface{}
}

StrFaceMap 表示一个以字符串为键,任意类型为值的映射

type StrFloatKV

type StrFloatKV KeyValueMode[string, float64]

StrFloatKV 表示一个以字符串为键,浮点数为值的键值对

type StrFloatMap

type StrFloatMap interface {
	map[string]float64
}

StrFloatMap 表示一个以字符串为键,浮点数为值的映射

type StrIntKV

type StrIntKV KeyValueMode[string, int]

StrIntKV 表示一个以字符串为键,整数为值的键值对

type StrIntMap

type StrIntMap interface {
	map[string]int
}

StrIntMap 表示一个以字符串为键,整数为值的映射

type StrMap

type StrMap interface {
	map[string]string
}

StrMap 表示一个以字符串为键,字符串为值的映射

type StrStrKV

type StrStrKV KeyValueMode[string, string]

StrStrKV 表示一个以字符串为键,字符串为值的键值对

type StrUintKV

type StrUintKV KeyValueMode[string, uint]

StrUintKV 表示一个以字符串为键,无符号整数为值的键值对

type StrUintMap

type StrUintMap interface {
	map[string]uint
}

StrUintMap 表示一个以字符串为键,无符号整数为值的映射

type TimeUnit added in v0.11.28

type TimeUnit int

TimeUnit 表示时间单位

const (
	Second TimeUnit = iota
	Minute
	Hour
	DayOfMonth
	Month
	DayOfWeek
	Year
)

type UintFaceKV

type UintFaceKV KeyValueMode[uint, interface{}]

UintFaceKV 表示一个以无符号整数为键,任意类型为值的键值对

type UintFaceMap

type UintFaceMap interface {
	map[uint]interface{}
}

UintFaceMap 表示一个以无符号整数为键,任意类型为值的映射

type UintFloatKV

type UintFloatKV KeyValueMode[uint, float64]

UintFloatKV 表示一个以无符号整数为键,浮点数为值的键值对

type UintFloatMap

type UintFloatMap interface {
	map[uint]float64
}

UintFloatMap 表示一个以无符号整数为键,浮点数为值的映射

type UintIntKV

type UintIntKV KeyValueMode[uint, int]

UintIntKV 表示一个以无符号整数为键,整数为值的键值对

type UintIntMap

type UintIntMap interface {
	map[uint]int
}

UintIntMap 表示一个以无符号整数为键,整数为值的映射

type UintMap

type UintMap interface {
	map[uint]uint
}

UintMap 表示一个以无符号整数为键,无符号整数为值的映射

type UintUintKV

type UintUintKV KeyValueMode[uint, uint]

UintUintKV 表示一个以无符号整数为键,无符号整数为值的键值对

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned 是一个约束,允许任何无符号整数类型。 如果未来的 Go 版本添加了新的预定义无符号整数类型, 这个约束将会被修改以包含它们。

Jump to

Keyboard shortcuts

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