Documentation
¶
Index ¶
- Variables
- func And(arg0 any, args ...any) bool
- func Append(a any, vals ...any) any
- func Bool(a any) bool
- func Cap(a any) int
- func Close(a any)
- func Compute(x any, symbol string, y any) (any, error)
- func Convert(a, b any) bool
- func Copy(a any, b any) any
- func CopyStruct(dsc, src any, exclude func(name string, dsc, src reflect.Value) bool) error
- func CopyStructDeep(dsc, src any, exclude func(name string, dsc, src reflect.Value) bool) error
- func Delete(m any, key any)
- func DepthField(s any, index ...any) (field any, err error)
- func EQ(a, b any) bool
- func GE(a, b any) bool
- func GT(a, b any) bool
- func Get(m any, key any) (any, error)
- func GetSlice(a any, start, end int) any
- func GetUnexported(m any, key any) (any, error)
- func Init(v any, isZero bool, args ...any) bool
- func LE(a, b any) bool
- func LT(a, b any) bool
- func Len(a any) int
- func Make(typ any, args ...any) any
- func MustType(v any) reflect.Type
- func NE(a, b any) bool
- func New(typ any) any
- func Not(a any) bool
- func Or(arg0 any, args ...any) bool
- func Panic(v any)
- func Recv(a any) (any, bool)
- func Register(name string, t reflect.Type)
- func Send(a any, v any)
- func Set(m any, args ...any) error
- func SetSlice(dst any, start, end int, src any) bool
- func SetUnexported(m any, args ...any) error
- func To(typ, v any) (any, error)
- func TryRecv(a any) (any, bool)
- func TrySend(a any, v any) bool
- func Type(v any) (reflect.Type, error)
- func Value(v any) reflect.Value
- type Chan
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilValue = errors.New("value is nil") ErrUnsupported = errors.New("unsupported type") ErrKeyType = errors.New("invalid key type") ErrIndexOutOfRange = errors.New("index out of range") ErrFieldNotFound = errors.New("field not found") ErrMapKey = errors.New("map key type mismatch") ErrInvalidArgCount = errors.New("invalid argument count: Set(obj, key1, val1, key2, val2, ...)") ErrUnaddressable = errors.New("value is unaddressable or field is unexported; pass a pointer to exported field") ErrNilMap = errors.New("cannot set on nil map") ErrImmutable = errors.New("immutable type (string/number) cannot be modified by index") )
Functions ¶
func Append ¶
Append 模拟内置 append 函数,支持任意切片类型和追加元素。 参数 a 必须是一个切片(可以是 nil 切片),vals 为待追加的值。 返回的新切片类型与原切片相同,若类型不匹配或 a 不是切片则会 panic。
func Compute ¶
Compute 对两个值进行动态算术/位运算。 支持:string(+)、有符号/无符号整数(+ - * / % & | ^ &^ << >>)、 float(+ - * /)、complex(+ - * /)。 两边必须具有相同的 reflect.Kind,结果类型与左操作数一致。 纯函数,并发安全。
func Copy ¶
Copy 模拟内置 copy 函数,支持任意切片类型及 string -> []byte。 参数 a 为目标(必须是切片),b 为源(切片或字符串)。 返回复制成功的元素个数(int),若类型不兼容则返回 0。
func CopyStruct ¶
func CopyStructDeep ¶
func Get ¶
Get 通用取值。
支持:
- map[K]V → key 可赋值/可转换/常见数字↔字符串互转
- slice / array → 整数索引(支持负索引)
- string → 整数索引(按字节,支持负索引)
- struct / *struct → 字段名(精确 / 首字母大小写 / json·mapstructure 标签)或整数索引(支持负索引)
- 数值类型 → 转为十进制字符串后按索引取字符
- 指针 / interface → 自动多层解包
- reflect.Value → 直接接受
行为约定:
- map 中不存在的 key → (nil, nil)
- 结构体找不到字段 → (nil, ErrFieldNotFound)
- 索引越界 → (nil, ErrIndexOutOfRange)
- 类型不支持 / key 类型错误 → 对应错误
func GetSlice ¶
GetSlice 返回 a[start:end] 的反射兼容版本。
语义严格对齐 Go 原生切片表达式:
- 要求 0 ≤ start ≤ end ≤ len(a)
- 对 slice / 可寻址 array / string:返回值与源共享底层数组(或字符串数据)
- 对不可寻址 array:返回独立拷贝,避免 panic
返回值约定:
- 成功时返回与源类型一致的切片(或子串)
- 任何越界、nil、不支持类型均返回 nil(interface{} 的 nil)
并发安全:
本函数本身无共享状态,可被多 goroutine 同时调用。 但当返回值与源共享底层数组时,调用方必须自行保证对源的并发写安全 (通常用 sync.Mutex / RWMutex 保护源 slice/array)。
支持类型:
- []T、*[N]T、[N]T、string
- 以及它们的任意层指针 / interface 包装(会自动解引用)
func Make ¶
Make 类似 Go 内置 make,支持:
Make([]T, length) / Make([]T, length, capacity) Make(map[K]V) / Make(map[K]V, size) Make(chan T) / Make(chan T, buffer) Make(func(...), impl) // 扩展:MakeFunc 风格(可选)
typ 可为:
- string 类型表达式("[]int", "map[string]int", "chan bool" 等)
- reflect.Type
- 具体值(取其类型)
负长度/容量会 panic(与 Go 一致)。 非 slice/map/chan/func 类型返回 nil 并带错误(或可选 panic)。
func Register ¶ added in v3.1.0
Register adds or replaces a named type in the registry. Safe for concurrent use.
func Set ¶
Set 通用赋值,与 Get 功能、错误、转换规则精确对称。
支持:
- map[K]V → key 可赋值/可转换/常见数字↔字符串互转;值为 nil 时删除键
- slice / array → 整数索引(支持负索引);slice 可自动扩容
- struct / *struct → 字段名(精确 / 首字母大小写 / json·mapstructure 标签)或整数索引(支持负索引)
- 指针 / interface → 自动多层解包
- reflect.Value → 直接接受(需最终可设置)
行为约定(与 Get 对应):
- 结构体找不到字段 → ErrFieldNotFound
- 索引越界 → ErrIndexOutOfRange
- 类型不支持 / key 类型错误 → ErrUnsupported / ErrKeyType / ErrMapKey
- 不可寻址或未导出字段 → ErrUnaddressable
- 字符串 / 数值按索引修改 → ErrImmutable
- map 为 nil → ErrNilMap
扩展行为:
- 写嵌入(Anonymous)指针路径时,路径上的 nil 嵌入指针会自动分配;普通指针字段不会
- 整次 Set 失败时会回滚:恢复已写叶子字段,并清除本次自动分配的中间嵌入指针, 保证失败后结构体与调用前完全一致(事务语义)
并发安全:函数本身无共享可变状态;字段索引使用 sync.Map 缓存(只读共享)。 对同一个 map / slice 的并发写仍需调用方加锁。 本函数不允许写入未导出字段(安全默认)。
func SetSlice ¶ added in v3.1.0
SetSlice 将 src 的全部内容严格写入 dst[start:end]。
严格语义:
- 0 ≤ start ≤ end ≤ len(dst)
- len(src) 必须精确等于 end-start,否则失败
- 元素类型必须完全可赋值(reflect.Copy 规则)
失败回滚保证:
任意校验失败都不会修改 dst。 即使在拷贝过程中出现异常(当前实现下几乎不会),也会自动还原原区间。
支持目标:[]T、[N]T 及其任意层 * / interface 包装 支持源: []T、[N]T、string(仅当目标元素为 byte)及其 * / interface 包装
返回 true 表示完整成功;false 表示失败且 dst 保持原样。
并发安全:函数本身无状态;共享底层数组时由调用方加锁。
func SetUnexported ¶ added in v3.1.0
SetUnexported 与 Set 行为完全一致,但允许通过 unsafe 写入未导出但可寻址的字段。 仅应在可信代码中使用。字段索引缓存按 (type, allow) 分别维护,与 Set 互不干扰。
func Value ¶
Value returns a zero value of the type described by v.
Accepted forms for v:
- string type name ("int", "string", "[]int", "map[string]int", "*float64", …)
- reflect.Type
- reflect.Value (uses its Type)
- any concrete value (uses TypeOf)
The returned Value is never a pointer unless the requested type itself is a pointer type.