maputils

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetValueByKey added in v1.1.5

func GetValueByKey[K comparable, V any](m map[K]any, key K) (V, bool)

GetValueByKey 通过key查询map的值 模板参数: K是map的key的类型,V是map值的类型 参数: m: 从这个map获取值 key: map的key 返回值: 对应的值和获取是否成功,失败可能有两个原因:对应的key不存在或者V类型转换失败

func GetValueByPath added in v1.1.5

func GetValueByPath(m map[string]any, path string) (any, error)

GetValueByPath 通过路径查询map的值,常用查找unmarshal到map的json或者yaml中的值 参数: m: key是string类型的map,值可以是任意类型,如果map下的值还是map,则map的key必须为string类型 path: 路径,如foo1.foo1_sub.[0],key为foo1的值是一个map[string]any,foo1_sub对应的值是一个slice,如果需要取slice的某一个值,可以用[n]获取 返回值: 对应的值和错误

Types

type SortMap added in v1.1.5

type SortMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SortMap 可以排序的map 模板参数: K是map的key的类型,V是map值的类型

func Sort added in v1.1.5

func Sort[K comparable, V any](m map[K]V, sortKeysFunc func(keys []K)) *SortMap[K, V]

Sort 排序map 模板参数: K是map的key的类型,V是map值的类型 参数: m: 需要排序的map sortKeysFunc: 对key进行排序的回调函数 返回值: SortMap指针,之后可以调用Range遍历排序后的map

func (*SortMap[K, V]) Range added in v1.1.5

func (sortMap *SortMap[K, V]) Range(callback func(index int, key K, value V))

Range 遍历排序后的map 模板参数: K是map的key的类型,V是map值的类型 参数: callback: 遍历回调

Jump to

Keyboard shortcuts

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