Documentation ¶
Overview ¶
包gutil提供了实用工具函数。
Index ¶
- func FillStructWithDefault(structPtr interface{}) error
- func Go(ctx context.Context, 协程函数 func(上下文 context.Context), ...)
- func ItemValue(item interface{}, key interface{}) (value interface{}, found bool)
- func ListItemValues(list interface{}, key interface{}, subKey ...interface{}) (values []interface{})
- func ListItemValuesUnique(list interface{}, key string, subKey ...interface{}) []interface{}
- func ListToMapByKey(list []map[string]interface{}, key string) map[string]interface{}
- func MapContains(data map[string]interface{}, key string) (ok bool)
- func MapContainsPossibleKey(data map[string]interface{}, key string) bool
- func MapCopy(data map[string]interface{}) (copy map[string]interface{})
- func MapDelete(data map[string]interface{}, keys ...string)
- func MapMerge(dst map[string]interface{}, src ...map[string]interface{})
- func MapMergeCopy(src ...map[string]interface{}) (copy map[string]interface{})
- func MapOmitEmpty(data map[string]interface{})
- func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{})
- func MapToSlice(data interface{}) []interface{}
- func SliceCopy(slice []interface{}) []interface{}
- func SliceDelete(slice []interface{}, index int) (newSlice []interface{})
- func SliceInsertAfter(slice []interface{}, index int, values ...interface{}) (newSlice []interface{})
- func SliceInsertBefore(slice []interface{}, index int, values ...interface{}) (newSlice []interface{})
- func SliceToMap(slice interface{}) map[string]interface{}
- func SliceToMapWithColumnAsKey(slice interface{}, key interface{}) map[interface{}]interface{}
- func X取值或取默认值(默认值 interface{}, 待判断变量 ...interface{}) interface{}
- func X取所有值(map或Struct interface{}) (值数组 []interface{})
- func X取所有名称(map或Struct interface{}) (键或属性 []string)
- func X取文本值或取默认值(默认值 string, 待判断变量 ...string) string
- func X异常捕捉(上下文 context.Context, 处理函数 func(上下文 context.Context)) (错误 error)
- func X异常捕捉并带异常处理(上下文 context.Context, 处理函数 func(上下文 context.Context), ...)
- func X异常输出(消息 interface{})
- func X是否为空(值 interface{}) bool
- func X比较字符(a, b interface{}) int
- func X比较字节(a, b interface{}) int
- func X比较小数32位(a, b interface{}) int
- func X比较小数64位(a, b interface{}) int
- func X比较整数(a, b interface{}) int
- func X比较整数16位(a, b interface{}) int
- func X比较整数32位(a, b interface{}) int
- func X比较整数64位(a, b interface{}) int
- func X比较整数8位(a, b interface{}) int
- func X比较文本(a, b interface{}) int
- func X比较时间(a, b interface{}) int
- func X比较正整数(a, b interface{}) int
- func X比较正整数16位(a, b interface{}) int
- func X比较正整数32位(a, b interface{}) int
- func X比较正整数64位(a, b interface{}) int
- func X比较正整数8位(a, b interface{}) int
- func X深拷贝(变量 interface{}) (新拷贝值 interface{})
- func X结构体到数组(结构体指针 interface{}) []interface{}
- func X调试输出(值s ...interface{})
- func X调试输出json(json值 string)
- func X调试输出到Writer(writer io.Writer, 值 interface{}, 选项 DumpOption)
- func X调试输出并带类型(值s ...interface{})
- func X调试输出并带选项(值 interface{}, 选项 DumpOption)
- type Comparator
- type DumpOption
- type OriginTypeAndKindOutput
- type OriginValueAndKindOutput
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FillStructWithDefault ¶
func FillStructWithDefault(structPtr interface{}) error
FillStructWithDefault 用 `default/d` 标签的值填充指针指向的结构体属性。 参数 `structPtr` 应为 *struct 或 []*struct 类型。
func Go ¶
func Go( ctx context.Context, 协程函数 func(上下文 context.Context), 异常处理函数 func(上下文 context.Context, 错误 error), )
Go 创建了一个具有指定恢复函数的新的异步 goroutine 函数。
参数 `recoverFunc` 在执行 `goroutineFunc` 期间发生任何 panic 时被调用。 如果 `recoverFunc` 被赋予 nil,它将忽略来自 `goroutineFunc` 的 panic,并且不会向父级 goroutine 抛出 panic。
但是请注意,如果 `recoverFunc` 也抛出了 panic,这样的 panic 将会被抛给父级 goroutine。
func ItemValue ¶
func ItemValue(item interface{}, key interface{}) (value interface{}, found bool)
ItemValue 通过 `key` 参数指定的名称/属性获取并返回其对应的值。 参数 `item` 可以是 map/*map/struct/*struct 类型。
func ListItemValues ¶
func ListItemValues(list interface{}, key interface{}, subKey ...interface{}) (values []interface{})
ListItemValues 通过键 `key` 获取并返回所有项(item)结构体或映射中的元素。 注意,参数 `list` 应为包含映射或结构体元素的切片类型,否则将返回一个空切片。
参数 `list` 支持以下类型: []map[string]interface{} []map[string]子映射 []struct []struct:子结构体 注意,只有当提供可选参数 `subKey` 时,子映射/子结构体才有意义。
func ListItemValuesUnique ¶
func ListItemValuesUnique(list interface{}, key string, subKey ...interface{}) []interface{}
ListItemValuesUnique 通过键 `key` 获取并返回所有结构体或映射中的唯一元素。 注意,参数 `list` 应为包含映射或结构体元素的切片类型, 否则将返回一个空切片。
func ListToMapByKey ¶
ListToMapByKey 将 `list` 转换为一个 map[string]interface{},其中键由 `key` 指定。 注意,项的值可能为 slice 类型。
func MapContains ¶
MapContains 检查字典 `data` 是否包含键 `key`。
func MapContainsPossibleKey ¶
MapContainsPossibleKey 检查给定的 `key` 是否包含在给定的映射 `data` 中。 它在忽略大小写和符号的情况下检查键值。
注意,此函数可能性能较低。
func MapMergeCopy ¶
MapMergeCopy 创建并返回一个新的映射,该映射将合并来自 `src` 中的所有映射。
func MapPossibleItemByKey ¶
func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{})
MapPossibleItemByKey尝试通过忽略大小写和符号的方式,为给定的键查找可能的键值对。
注意:此函数可能性能较低。
func MapToSlice ¶
func MapToSlice(data interface{}) []interface{}
MapToSlice 将映射转换为切片,其中所有键和值都是其元素。 例如:{"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
func SliceCopy ¶
func SliceCopy(slice []interface{}) []interface{}
SliceCopy 对于最常用的切片类型 []interface{},执行浅拷贝操作,复制 `data` 切片。
func SliceDelete ¶
func SliceDelete(slice []interface{}, index int) (newSlice []interface{})
SliceDelete 在`index`处删除一个元素并返回新的切片。 如果给定的`index`无效,则不做任何操作。
func SliceInsertAfter ¶
func SliceInsertAfter(slice []interface{}, index int, values ...interface{}) (newSlice []interface{})
SliceInsertAfter 在 `index` 后面插入 `values`,并返回一个新的切片。
Example ¶
s1 := g.Slice别名{ 0, 1, 2, 3, 4, } s2 := 工具类.SliceInsertAfter(s1, 1, 8, 9) fmt.Println(s1) fmt.Println(s2)
Output: [0 1 2 3 4] [0 1 8 9 2 3 4]
func SliceInsertBefore ¶
func SliceInsertBefore(slice []interface{}, index int, values ...interface{}) (newSlice []interface{})
SliceInsertBefore 将 `values` 插入到 `index` 位置之前,并返回一个新的切片。
Example ¶
s1 := g.Slice别名{ 0, 1, 2, 3, 4, } s2 := 工具类.SliceInsertBefore(s1, 1, 8, 9) fmt.Println(s1) fmt.Println(s2)
Output: [0 1 2 3 4] [0 8 9 1 2 3 4]
func SliceToMap ¶
func SliceToMap(slice interface{}) map[string]interface{}
SliceToMap 将切片类型变量 `slice` 转换为 `map[string]interface{}` 类型。 注意,如果 `slice` 的长度不是偶数,则返回 nil。 示例: ["K1", "v1", "K2", "v2"] => {"K1": "v1", "K2": "v2"} ["K1", "v1", "K2"] => nil
func SliceToMapWithColumnAsKey ¶
func SliceToMapWithColumnAsKey(slice interface{}, key interface{}) map[interface{}]interface{}
SliceToMapWithColumnAsKey 将切片类型变量 `slice` 转换为 `map[interface{}]interface{}` 指定列的值作为返回映射中的键。 示例: SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K1") => {"v1": {"K1": "v1", "K2": 1}, "v2": {"K1": "v2", "K2": 2}} SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K2") => {1: {"K1": "v1", "K2": 1}, 2: {"K1": "v2", "K2": 2}} 这段Go语言代码注释翻译成中文后,其内容如下: SliceToMapWithColumnAsKey 函数将 slice 类型变量转换为 map 类型变量,其中 map 的键是指定列的值。 例如: 当调用 SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K1") 时,返回结果为 {"v1": {"K1": "v1", "K2": 1}, "v2": {"K1": "v2", "K2": 2}} 当调用 SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K2") 时,返回结果为 {1: {"K1": "v1", "K2": 1}, 2: {"K1": "v2", "K2": 2}}
func X取值或取默认值 ¶
func X取值或取默认值(默认值 interface{}, 待判断变量 ...interface{}) interface{}
GetOrDefaultAny 检查并根据参数 `param` 是否可用返回值。 如果 `param[0]` 可用,它将返回 `param[0]`;否则返回 `def`。
func X取所有值 ¶
func X取所有值(map或Struct interface{}) (值数组 []interface{})
Values 从给定的 map 或 struct 中获取并返回其值。
func X取所有名称 ¶
func X取所有名称(map或Struct interface{}) (键或属性 []string)
Keys 从给定的 map 或 struct 中获取并返回其键(key)。
func X取文本值或取默认值 ¶
GetOrDefaultStr 检查并根据参数 `param` 是否可用返回值。 如果 `param[0]` 可用,返回 `param[0]`,否则返回 `def`。
func X异常捕捉并带异常处理 ¶
func X异常捕捉并带异常处理(上下文 context.Context, 处理函数 func(上下文 context.Context), 异常处理函数 func(上下文 context.Context, 错误 error))
TryCatch 实现了类似 `try...catch...` 的逻辑,通过内部使用 `panic...recover`。 如果发生任何异常,它会自动调用函数 `catch` 并将异常作为 error 传递给 `catch` 函数。 若传入的 `catch` 为 nil,则忽略来自 `try` 的 panic,并且不会向父级 goroutine 抛出 panic。
但是请注意,如果函数 `catch` 本身也抛出了 panic,则当前 goroutine 将会 panic。
func X是否为空 ¶
func X是否为空(值 interface{}) bool
IsEmpty 检查给定的 `value` 是否为空。 当 `value` 为:整数(0)、布尔值(false)、切片/映射(len=0)或 nil 时,返回 false; 否则返回 true。
func X深拷贝 ¶
func X深拷贝(变量 interface{}) (新拷贝值 interface{})
Copy 返回v的深度拷贝。
Copy无法复制结构体中的未导出字段(小写字段名)。 未导出字段不能被Go运行时反射,因此无法执行任何数据拷贝操作。
func X结构体到数组 ¶
func X结构体到数组(结构体指针 interface{}) []interface{}
StructToSlice 将结构体转换为键值对构成的切片。 例如:{"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
func X调试输出到Writer ¶
func X调试输出到Writer(writer io.Writer, 值 interface{}, 选项 DumpOption)
DumpTo 将变量 `values` 转换为字符串并写入到 `writer` 中,以更易于人工阅读的方式
func X调试输出并带选项 ¶
func X调试输出并带选项(值 interface{}, 选项 DumpOption)
DumpWithOption 使用自定义选项返回变量 `values`,将其格式化为更易读的字符串形式。
Types ¶
type Comparator ¶
type Comparator func(a, b interface{}) int
Comparator 是一个函数,用于比较 a 和 b,并返回一个整数形式的结果。
应该返回一个数字:
如果 a < b,则返回负数 如果 a == b,则返回零 如果 a > b,则返回正数
type DumpOption ¶
type DumpOption struct { WithType bool // WithType 指定在导出内容时包含类型信息。 ExportedOnly bool // 只导出结构体的公开字段。 }
DumpOption 定义了 Export 函数的行为。
type OriginTypeAndKindOutput ¶
type OriginTypeAndKindOutput = reflection.OriginTypeAndKindOutput
func OriginTypeAndKind ¶
func OriginTypeAndKind(value interface{}) (out OriginTypeAndKindOutput)
OriginTypeAndKind 获取并返回原始 reflect 类型和种类。
type OriginValueAndKindOutput ¶
type OriginValueAndKindOutput = reflection.OriginValueAndKindOutput
func OriginValueAndKind ¶
func OriginValueAndKind(value interface{}) (out OriginValueAndKindOutput)
OriginValueAndKind 获取并返回原始 reflect 值和类型。