types

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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-11 13:17:27 * @FilePath: \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"
)

Functions

func CheckTypeCompatibility

func CheckTypeCompatibility(srcType, dstType reflect.Type) error

CheckTypeCompatibility 检查类型兼容性

func Chunk

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

Chunk 将切片分块

func Contains

func Contains[T comparable](slice []T, value T) bool

Contains 检查值是否在切片中(泛型版本,支持任意可比较类型)

func ContainsAll

func ContainsAll[T comparable](slice []T, values ...T) bool

ContainsAll 检查切片中是否包含所有目标值

func ContainsAny

func ContainsAny[T comparable](slice []T, values ...T) bool

ContainsAny 检查切片中是否包含任意一个目标值

func EnsureStructDefaults

func EnsureStructDefaults(v reflect.Value)

EnsureStructDefaults 确保结构体字段默认值

func ExtractGormColumn

func ExtractGormColumn(tag string) string

ExtractGormColumn 从 gorm tag 中提取 column 名

func ExtractJSONKey

func ExtractJSONKey(fieldType reflect.StructField) string

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

func ExtractPBTagValue

func ExtractPBTagValue(tag string, key string) string

ExtractPBTagValue 从 protobuf tag 中提取指定键的值

func Filter

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

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

func HasJSONTagOption

func HasJSONTagOption(fieldType reflect.StructField, options ...string) bool

HasJSONTagOption 判断结构体字段的 json tag 是否包含指定选项

func IndexOf

func IndexOf[T comparable](slice []T, value T) int

IndexOf 返回值在切片中的索引,不存在返回 -1

func IsExportedField

func IsExportedField(field reflect.StructField) bool

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

func JSONFieldName

func JSONFieldName(fieldType reflect.StructField) string

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

func MapTR

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

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

func ResolveModelKey

func ResolveModelKey(fieldType reflect.StructField) string

ResolveModelKey 解析 Model 结构体字段的 key 优先使用 gorm column tag,其次 json tag,最后使用字段名

func ResolvePBKey

func ResolvePBKey(fieldType reflect.StructField) string

ResolvePBKey 解析 PB 结构体字段的 key 优先使用 protobuf tag 的 name,其次 json tag,最后使用字段名

func Reverse

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

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

func ToFloat64OK

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

ToFloat64OK 尝试将值转换为 float64 类型,返回转换结果和是否成功

func Unique

func Unique[T comparable](slices ...[]T) []T

Unique 去重切片(保持原始顺序),支持合并多个切片后去重 性能优化: - 使用 map 快速查重,每次查找 O(1),总体时间复杂度 O(n) - 预分配合理容量减少内存分配 - 单次遍历完成去重

func UnwrapModelValue

func UnwrapModelValue(iface interface{}) interface{}

UnwrapModelValue 解引用指针类型,返回底层值 如果不是指针或指针为 nil,返回原始值

Types

type BoundError

type BoundError int

BoundError 边界错误类型

const (
	BoundErrorNone         BoundError = iota // 无错误
	BoundErrorBelowMin                       // 低于最小值
	BoundErrorAboveMax                       // 超过最大值
	BoundErrorInvalidRange                   // 无效范围(min > max)
	BoundErrorZeroStep                       // 步长为零
	BoundErrorNegative                       // 负数错误
)

type BoundType

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

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

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

type RangeMode int

RangeMode 范围解析模式

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

type RangeParser

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

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

type RangeTransformer

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

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

type RangeValidator

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

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