Documentation ¶
Index ¶
- func ContainKeys[K comparable, V any](m map[K]V, keys ...K) bool
- func Contains[K comparable, V any](m map[K]V, key K) bool
- func Copy[K comparable, V any](src map[K]V) map[K]V
- func Decode(input interface{}, output interface{}) error
- func Get[K comparable, V any](m map[K]V, key K) V
- func GetBool[K comparable, V any](m map[K]V, key K) (bool, error)
- func GetInt[K comparable, V any](m map[K]V, key K) (int, error)
- func GetInt32[K comparable, V any](m map[K]V, key K) (int32, error)
- func GetInt64[K comparable, V any](m map[K]V, key K) (int64, error)
- func GetString[K comparable, V any](m map[K]V, key K) (string, error)
- func Keys[K comparable, V any](m map[K]V) []K
- func MapToSlice[K comparable, V any, R any](m map[K]V, transform func(key K, value V) R) []R
- func Merge[K comparable, V any](maps ...map[K]V) map[K]V
- func NewAnyAnyMap(safe ...bool) *gmap.AnyAnyMap
- func NewAnyAnyMapFrom(data map[interface{}]interface{}, safe ...bool) *gmap.AnyAnyMap
- func NewIntAnyMap(safe ...bool) *gmap.IntAnyMap
- func NewIntAnyMapFrom(data map[int]interface{}, safe ...bool) *gmap.IntAnyMap
- func NewIntIntMap(safe ...bool) *gmap.IntIntMap
- func NewIntIntMapFrom(data map[int]int, safe ...bool) *gmap.IntIntMap
- func NewIntStrMap(safe ...bool) *gmap.IntStrMap
- func NewIntStrMapFrom(data map[int]string, safe ...bool) *gmap.IntStrMap
- func NewListMap(safe ...bool) *gmap.ListMap
- func NewListMapFrom(data map[interface{}]interface{}, safe ...bool) *gmap.ListMap
- func NewStrAnyMap(safe ...bool) *gmap.StrAnyMap
- func NewStrAnyMapFrom(data map[string]interface{}, safe ...bool) *gmap.StrAnyMap
- func NewStrIntMap(safe ...bool) *gmap.StrIntMap
- func NewStrIntMapFrom(data map[string]int, safe ...bool) *gmap.StrIntMap
- func NewStrStrMap(safe ...bool) *gmap.StrStrMap
- func NewStrStrMapFrom(data map[string]string, safe ...bool) *gmap.StrStrMap
- func NewTreeMap(comparator func(v1, v2 interface{}) int, safe ...bool) *gmap.TreeMap
- func NewTreeMapFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, ...) *gmap.TreeMap
- func Remove[K comparable, V any](m map[K]V, key K) (V, bool)
- func Set[K comparable, V any](m map[K]V, key K, value V)
- func SetSafely[K comparable, V any](m map[K]V, key K, value V) map[K]V
- func Values[K comparable, V any](m map[K]V) []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainKeys ¶
func ContainKeys[K comparable, V any](m map[K]V, keys ...K) bool
ContainKeys 判断 map实例 中是否存在 所有指定的key
@param m 可以为nil(此时返回值固定为false) @param keys 可以一个key都不传,此时将固定返回true
func Contains ¶
func Contains[K comparable, V any](m map[K]V, key K) bool
Contains 判断 map实例 中是否存在 指定的key.
@param m 可以为nil(此时返回值固定为false)
e.g. (map[string]interface{}(nil), "1") => false
func Copy ¶ added in v2.0.17
func Copy[K comparable, V any](src map[K]V) map[K]V
Copy 浅拷贝
@param src 可以为nil @return 保底为空的map实例(不为nil)
func Decode ¶
func Decode(input interface{}, output interface{}) error
Decode 将 通用的map[string]interface{} 解码到对应的 Go结构体中 ,或者执行相反的操作。
PS: (1) 也可用于: map转map; (2) 字段标签(tag)不同于json,详见: https://mp.weixin.qq.com/s/n_AXYv-p_ev-q23mhpDkOg .
@param input structure(包括map) @param output 必须是(map或结构体的)指针
func Get ¶
func Get[K comparable, V any](m map[K]V, key K) V
Get
@param m 可以为nil @param key 如果不存在对应的值,将返回值类型的零值
func GetBool ¶
func GetBool[K comparable, V any](m map[K]V, key K) (bool, error)
GetBool
@param m 可以为nil
func GetInt ¶
func GetInt[K comparable, V any](m map[K]V, key K) (int, error)
GetInt
@param m 可以为nil
func GetInt32 ¶
func GetInt32[K comparable, V any](m map[K]V, key K) (int32, error)
GetInt32
@param m 可以为nil
func GetInt64 ¶
func GetInt64[K comparable, V any](m map[K]V, key K) (int64, error)
GetInt64
@param m 可以为nil
func GetString ¶
func GetString[K comparable, V any](m map[K]V, key K) (string, error)
GetString
@param m 可以为nil
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys creates an array of the map keys.
@param m 可以为nil @return 保底空的slice实例
func MapToSlice ¶
func MapToSlice[K comparable, V any, R any](m map[K]V, transform func(key K, value V) R) []R
MapToSlice map实例 => slice实例
@param m 可以为nil @param transform (1) 不能为nil(除非m == nil),否则会导致panic: runtime error: invalid memory address or nil pointer dereference
(2) 传参: 一对键值,返回值: slice实例中的一个元素
@return 必定不为nil(保底为空的slice实例)
e.g.
s := mapKit.MapToSlice[string, string, string](map[string]string{"1": "a"}, func(key string, value string) string { return key + value }) fmt.Println(s) // [1a]
func Merge ¶
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
Merge 合并多个map实例
PS: 存在key相同的情况,以最右边的为准.
@param maps (1)可以为nil
(2)内部可以有nil(甚至全是nil) (3)不会修改其中任何一个map实例
@return (1)保底为空的map实例(不为nil)
(2)返回的是一个新的map实例
e.g.
m := mapKit.Merge[string, int]( map[string]int{"a": 1, "b": 2}, map[string]int{"a": 10}, map[string]int{"a": 100}, ) fmt.Println(m) // map[a:100 b:2]
func NewAnyAnyMap ¶ added in v2.0.18
func NewAnyAnyMapFrom ¶ added in v2.0.18
func NewIntAnyMap ¶ added in v2.0.18
func NewIntAnyMapFrom ¶ added in v2.0.18
func NewIntIntMap ¶ added in v2.0.18
func NewIntIntMapFrom ¶ added in v2.0.18
func NewIntStrMap ¶ added in v2.0.18
func NewIntStrMapFrom ¶ added in v2.0.18
func NewListMapFrom ¶ added in v2.0.18
func NewStrAnyMap ¶ added in v2.0.18
func NewStrAnyMapFrom ¶ added in v2.0.18
func NewStrIntMap ¶ added in v2.0.18
func NewStrIntMapFrom ¶ added in v2.0.18
func NewStrStrMap ¶ added in v2.0.18
func NewStrStrMapFrom ¶ added in v2.0.18
func NewTreeMap ¶ added in v2.0.18
NewTreeMap
PS: 当需要让返回结果按照自然升序排列时使用TreeMap.
@param comparator e.g. gutil.ComparatorInt
func NewTreeMapFrom ¶ added in v2.0.18
func Remove ¶
func Remove[K comparable, V any](m map[K]V, key K) (V, bool)
Remove
PS: (1) 可能会修改传参m(移除条目的话),因为它是map类型; (2) 命名参考了 java.util.Map .
@param m 可以为nil @param key 可以在传参m中不存在 @return 被移除出map的条目的值(存在的话) + 传参m是否包含传参key
e.g.
m := map[string]interface{}{ "a": 0, "b": 1, "c": 2, } fmt.Println(mapKit.Remove(m, "b")) // 1 true fmt.Println(m) // map[a:0 c:2]
func Set ¶
func Set[K comparable, V any](m map[K]V, key K, value V)
Set 设置值(或更新值)
@param m 不能为nil(否则会导致 panic: assignment to entry in nil map)
func SetSafely ¶
func SetSafely[K comparable, V any](m map[K]V, key K, value V) map[K]V
SetSafely 设置值(或更新值)
@param m 可以为nil @return 可能是一个新的map实例
func Values ¶
func Values[K comparable, V any](m map[K]V) []V
Values creates an array of the map values.
@param m 可以为nil @return 保底空的slice实例
Types ¶
This section is empty.