builtin

package
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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 And

func And(arg0 any, args ...any) bool

&&

func Append

func Append(a any, vals ...any) any

Append 模拟内置 append 函数,支持任意切片类型和追加元素。 参数 a 必须是一个切片(可以是 nil 切片),vals 为待追加的值。 返回的新切片类型与原切片相同,若类型不匹配或 a 不是切片则会 panic。

func Bool

func Bool(a any) bool

Bool returns bool(a)

func Cap

func Cap(a any) int

Cap returns the capacity of a, or 0 if a is not an array, slice, or channel.

func Close

func Close(a any)

关闭通道 Close(*Chan/reflect.Value/chan T)

func Compute

func Compute(x any, symbol string, y any) (any, error)

Compute 对两个值进行动态算术/位运算。 支持:string(+)、有符号/无符号整数(+ - * / % & | ^ &^ << >>)、 float(+ - * /)、complex(+ - * /)。 Kind 不同但均为数值时,按左操作数类型转换。 纯函数,并发安全。

func Convert

func Convert(a, b any) bool

func Copy

func Copy(a any, b any) any

func CopyStruct

func CopyStruct(dsc, src any, exclude func(name string, dsc, src reflect.Value) bool) error

CopyStruct 浅复制:src 字段为空(nil 指针/slice/map 等)时不覆盖 dst。

func CopyStructDeep

func CopyStructDeep(dsc, src any, exclude func(name string, dsc, src reflect.Value) bool) error

CopyStructDeep 深复制:src 字段为空也写入 dst(结果可为 nil)。

func Delete

func Delete(m any, key any)

Delete 删除 map 中的键。

func DepthField

func DepthField(s any, index ...any) (field any, err error)

func EQ

func EQ(a, b any) (eq bool)

a == b

func GE

func GE(a, b any) bool

a >= b

func GT

func GT(a, b any) bool

a > b

func Get

func Get(m any, key any) (any, error)

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

func GetSlice(a any, start, end int) (out any)

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 GetUnexported added in v3.1.0

func GetUnexported(m any, key any) (any, error)

func Init

func Init(v any, isZero bool, args ...any) bool

Init 对已有值做指针链分配,可选把最终值置零/做成容器,并在 isZero 时补结构体里的 nil 指针。 与 Make 职责分离,解决原 initChain 与 Make 混用问题。

func LE

func LE(a, b any) bool

a <= b

func LT

func LT(a, b any) bool

a < b

func Len

func Len(a any) int

func Make

func Make(typ any, args ...any) any

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 make 更接近)。

Example
s := Make("[]string", 2, 4).([]string)
m := Make(map[string]int{}).(map[string]int)
ch := Make("chan bool").(chan bool)

fmt.Printf("slice len=%d cap=%d\n", len(s), cap(s))
fmt.Printf("map nil? %v\n", m == nil)
fmt.Printf("chan cap=%d\n", cap(ch))
Output:
slice len=2 cap=4
map nil? false
chan cap=0

func MustType added in v3.1.0

func MustType(v any) reflect.Type

MustType is like Type but panics on error.

func NE

func NE(a, b any) bool

a != b

func New added in v3.1.0

func New(typ any) any

New 返回 *T 的可设置指针(类似内置 new)。

func Not

func Not(a any) bool

!1

func Or

func Or(arg0 any, args ...any) bool

||

func Panic

func Panic(v any)

Panic(v)

func Recv

func Recv(a any) (any, bool)

阻塞接收 Recv 返回 (值, 通道是否未关闭) ok == false 说明 channel 已关闭

func Register added in v3.1.0

func Register(name string, t reflect.Type)

Register adds or replaces a named type in the registry. Safe for concurrent use.

func Send

func Send(a any, v any)

阻塞发送 Send(*Chan/reflect.Value/chan T, value)

func Set

func Set(m any, args ...any) error

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

func SetSlice(dst any, start, end int, src any) (ok bool)

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

func SetUnexported(m any, args ...any) error

SetUnexported 与 Set 行为完全一致,但允许通过 unsafe 写入未导出但可寻址的字段。 仅应在可信代码中使用。字段索引缓存按 (type, allow) 分别维护,与 Set 互不干扰。

func To added in v3.2.0

func To(typ, v any) (any, error)

func TryRecv

func TryRecv(a any) (val any, ok bool)

不阻塞 TryRecv 返回 (值, 是否成功接收) ok == false 说明 channel 为空或已关闭

func TrySend

func TrySend(a any, v any) (ok bool)

不阻塞 TrySend(*Chan/reflect.Value/chan T, value)

func Type

func Type(v any) (reflect.Type, error)

Type returns the reflect.Type described by v.

func Value

func Value(v any) reflect.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.

Types

type Chan

type Chan struct {
	C reflect.Value
}

Jump to

Keyboard shortcuts

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