Map - Methods - 图1tip

The following list of common methods may lag behind the features of the code in the documentation update. Please refer to the code documentation for more methods and examples: https://pkg.go.dev/github.com/gogf/gf/v2/container/gmap

New

  • Description: New creates and returns an empty AnyAnyMap. The parameter safe is used to specify whether to use a concurrency-safe map, which is false by default.

  • Format:

  1. New(safe ...bool) *Map
  • Example:
  1. func ExampleNew() {
  2. m := gmap.New()
  3. // Add data.
  4. m.Set("key1", "val1")
  5. // Print size.
  6. fmt.Println(m.Size())
  7. addMap := make(map[interface{}]interface{})
  8. addMap["key2"] = "val2"
  9. addMap["key3"] = "val3"
  10. addMap[1] = 1
  11. fmt.Println(m.Values())
  12. // Batch add data.
  13. m.Sets(addMap)
  14. // Gets the value of the corresponding key.
  15. fmt.Println(m.Get("key3"))
  16. // Get the value by key, or set it with given key-value if not exist.
  17. fmt.Println(m.GetOrSet("key4", "val4"))
  18. // Set key-value if the key does not exist, then return true; or else return false.
  19. fmt.Println(m.SetIfNotExist("key3", "val3"))
  20. // Remove key
  21. m.Remove("key2")
  22. fmt.Println(m.Keys())
  23. // Batch remove keys.
  24. m.Removes([]interface{}{"key1", 1})
  25. fmt.Println(m.Keys())
  26. // Contains checks whether a key exists.
  27. fmt.Println(m.Contains("key3"))
  28. // Flip exchanges key-value of the map, it will change key-value to value-key.
  29. m.Flip()
  30. fmt.Println(m.Map())
  31. // Clear deletes all data of the map.
  32. m.Clear()
  33. fmt.Println(m.Size())
  34. // May Output:
  35. // 1
  36. // [val1]
  37. // val3
  38. // val4
  39. // false
  40. // [key4 key1 key3 1]
  41. // [key4 key3]
  42. // true
  43. // map[val3:key3 val4:key4]
  44. // 0
  45. }

NewFrom

  • Description: NewFrom creates and returns an AnyAnyMap with the data of the given map.

  • Note: The input parameter map will be set as the underlying data mapping (without deep copy), so external changes to the map may introduce some safety issues simultaneously. Optional argument safe specifies whether to use this structure in concurrency safety, default is false.

  • Format:

  1. NewFrom(data map[interface{}]interface{}, safe ...bool) *Map
  • Example:
  1. func ExampleNewFrom() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m)
  5. n := gmap.NewFrom(m.MapCopy(), true)
  6. fmt.Println(n)
  7. // Output:
  8. // {"key1":"val1"}
  9. // {"key1":"val1"}
  10. }

Iterator

  • Description: Iterator iterates through the hashmap in a read-only manner using a custom callback function f. If f returns true, it continues iterating, if it returns false, it stops.

  • Format:

  1. Iterator(f func(k interface{}, v interface{}) bool)
  • Example:
  1. func ExampleAnyAnyMap_Iterator() {
  2. m := gmap.New()
  3. for i := 0; i < 10; i++ {
  4. m.Set(i, i*2)
  5. }
  6. var totalKey, totalValue int
  7. m.Iterator(func(k interface{}, v interface{}) bool {
  8. totalKey += k.(int)
  9. totalValue += v.(int)
  10. return totalKey < 10
  11. })
  12. fmt.Println("totalKey:", totalKey)
  13. fmt.Println("totalValue:", totalValue)
  14. // May Output:
  15. // totalKey: 11
  16. // totalValue: 22
  17. }

Clone

  • Description: Clone returns a new AnyAnyMap, which contains a copy of the current map data.

  • Format:

  1. Clone(safe ...bool) *AnyAnyMap
  • Example:
  1. func ExampleAnyAnyMap_Clone() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m)
  5. n := m.Clone()
  6. fmt.Println(n)
  7. // Output:
  8. // {"key1":"val1"}
  9. // {"key1":"val1"}
  10. }

Map

  • Description: Map returns the underlying data map.

  • Note: If in concurrency safety, it returns a copy of the underlying data, otherwise it returns a pointer pointing to the underlying data.

  • Format:

  1. Map() map[interface{}]interface{}
  • Example:
  1. func ExampleAnyAnyMap_Map() {
  2. // non concurrent-safety, a pointer to the underlying data
  3. m1 := gmap.New()
  4. m1.Set("key1", "val1")
  5. fmt.Println("m1:", m1)
  6. n1 := m1.Map()
  7. fmt.Println("before n1:", n1)
  8. m1.Set("key1", "val2")
  9. fmt.Println("after n1:", n1)
  10. // concurrent-safety, copy of underlying data
  11. m2 := gmap.New(true)
  12. m2.Set("key1", "val1")
  13. fmt.Println("m1:", m2)
  14. n2 := m2.Map()
  15. fmt.Println("before n2:", n2)
  16. m2.Set("key1", "val2")
  17. fmt.Println("after n2:", n2)
  18. // Output:
  19. // m1: {"key1":"val1"}
  20. // before n1: map[key1:val1]
  21. // after n1: map[key1:val2]
  22. // m1: {"key1":"val1"}
  23. // before n2: map[key1:val1]
  24. // after n2: map[key1:val1]
  25. }

MapCopy

  • Description: MapCopy returns a copy of the data in the map.

  • Format:

  1. MapCopy() map[interface{}]interface{}
  • Example:
  1. func ExampleAnyAnyMap_MapCopy() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. m.Set("key2", "val2")
  5. fmt.Println(m)
  6. n := m.MapCopy()
  7. fmt.Println(n)
  8. // Output:
  9. // {"key1":"val1","key2":"val2"}
  10. // map[key1:val1 key2:val2]
  11. }

MapStrAny

  • Description: MapStrAny returns a copy of the map‘s data in the form of map[string]interface{}.

  • Format:

  1. MapStrAny() map[string]interface{}
  • Example:
  1. func ExampleAnyAnyMap_MapStrAny() {
  2. m := gmap.New()
  3. m.Set(1001, "val1")
  4. m.Set(1002, "val2")
  5. n := m.MapStrAny()
  6. fmt.Println(n)
  7. // Output:
  8. // map[1001:val1 1002:val2]
  9. }

FilterEmpty

  • Description: FilterEmpty removes all key-value pairs with empty values. Values such as 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

  • Format:

  1. FilterEmpty()
  • Example:
  1. func ExampleAnyAnyMap_FilterEmpty() {
  2. m := gmap.NewFrom(g.MapAnyAny{
  3. "k1": "",
  4. "k2": nil,
  5. "k3": 0,
  6. "k4": 1,
  7. })
  8. m.FilterEmpty()
  9. fmt.Println(m.Map())
  10. // Output:
  11. // map[k4:1]
  12. }

FilterNil

  • Description: FilterNil removes all key-value pairs where the value is nil.

  • Format:

  1. FilterNil()
  • Example:
  1. func ExampleAnyAnyMap_FilterNil() {
  2. m := gmap.NewFrom(g.MapAnyAny{
  3. "k1": "",
  4. "k2": nil,
  5. "k3": 0,
  6. "k4": 1,
  7. })
  8. m.FilterNil()
  9. fmt.Println(m.Map())
  10. // May Output:
  11. // map[k1: k3:0 k4:1]
  12. }

Set

  • Description: Set sets the key/value for the map.

  • Format:

  1. Set(key interface{}, value interface{})
  • Example:
  1. func ExampleAnyAnyMap_Set() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m)
  5. // Output:
  6. // {"key1":"val1"}
  7. }

Sets

  • Description: Sets sets the key/ value batch for the map.

  • Format:

  1. Sets(data map[interface{}]interface{})
  • Example:
  1. func ExampleAnyAnyMap_Sets() {
  2. m := gmap.New()
  3. addMap := make(map[interface{}]interface{})
  4. addMap["key1"] = "val1"
  5. addMap["key2"] = "val2"
  6. addMap["key3"] = "val3"
  7. m.Sets(addMap)
  8. fmt.Println(m)
  9. // Output:
  10. // {"key1":"val1","key2":"val2","key3":"val3"}
  11. }

Search

  • Description: Search searches the map using the parameter key. If the key is found, it returns its corresponding value and the parameter found as true, otherwise as false.

  • Format:

  1. Search(key interface{}) (value interface{}, found bool)
  • Example:
  1. func ExampleAnyAnyMap_Search() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. value, found := m.Search("key1")
  5. if found {
  6. fmt.Println("find key1 value:", value)
  7. }
  8. value, found = m.Search("key2")
  9. if !found {
  10. fmt.Println("key2 not find")
  11. }
  12. // Output:
  13. // find key1 value: val1
  14. // key2 not find
  15. }

Get

  • Description: Get returns the value corresponding to the parameter key. If the key does not exist, it returns Nil.

  • Format:

  1. Get(key interface{}) (value interface{})
  • Example:
  1. func ExampleAnyAnyMap_Get() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println("key1 value:", m.Get("key1"))
  5. fmt.Println("key2 value:", m.Get("key2"))
  6. // Output:
  7. // key1 value: val1
  8. // key2 value: <nil>
  9. }

Pop

  • Description: Pop randomly retrieves and returns a key-value pair from map and deletes the key-value pair internally.

  • Format:

  1. Pop() (key, value interface{})
  • Example:
  1. func ExampleAnyAnyMap_Pop() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. fmt.Println(m.Pop())
  10. // May Output:
  11. // k1 v1
  12. }

Pops

  • Description: Pops randomly retrieves and deletes size number of key-value pairs from map. If size == -1, it deletes and returns all key-value pairs.

  • Format:

  1. Pops(size int) map[interface{}]interface{}
  • Example:
  1. func ExampleAnyAnyMap_Pops() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. fmt.Println(m.Pops(-1))
  10. fmt.Println("size:", m.Size())
  11. m.Sets(g.MapAnyAny{
  12. "k1": "v1",
  13. "k2": "v2",
  14. "k3": "v3",
  15. "k4": "v4",
  16. })
  17. fmt.Println(m.Pops(2))
  18. fmt.Println("size:", m.Size())
  19. // May Output:
  20. // map[k1:v1 k2:v2 k3:v3 k4:v4]
  21. // size: 0
  22. // map[k1:v1 k2:v2]
  23. // size: 2
  24. }

GetOrSet

  • Description: GetOrSet returns value if key exists. If key does not exist, it sets the key-value to the map and then returns the value.

  • Format:

  1. GetOrSet(key interface{}, value interface{}) interface{}
  • Example:
  1. func ExampleAnyAnyMap_GetOrSet() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetOrSet("key1", "NotExistValue"))
  5. fmt.Println(m.GetOrSet("key2", "val2"))
  6. // Output:
  7. // val1
  8. // val2
  9. }

GetOrSetFunc

  • Description: GetOrSetFunc returns value if key exists. If key does not exist, it sets the key-value for map using the return value of func f and then returns the value.

  • Format:

  1. GetOrSetFunc(key interface{}, f func() interface{}) interface{}
  • Example:
  1. func ExampleAnyAnyMap_GetOrSetFunc() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
  5. return "NotExistValue"
  6. }))
  7. fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
  8. return "NotExistValue"
  9. }))
  10. // Output:
  11. // val1
  12. // NotExistValue
  13. }

GetOrSetFuncLock

  • Description: GetOrSetFunc returns value if key exists. If key does not exist, it sets the key-value for map using the return value of func f and then returns the value.

  • Note: The difference between GetOrSetFuncLock and GetOrSetFunc is that it executes the function f in a write lock.

  • Format:

  1. GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
  • Example:
  1. func ExampleAnyAnyMap_GetOrSetFuncLock() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
  5. return "NotExistValue"
  6. }))
  7. fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
  8. return "NotExistValue"
  9. }))
  10. // Output:
  11. // val1
  12. // NotExistValue
  13. }

GetVar

  • Description: GetVar queries and returns the key-value corresponding to the key name key, with the key value returned using the generic type *gvar.Var.

  • Format:

  1. GetVar(key interface{}) *gvar.Var
  • Example:
  1. func ExampleAnyAnyMap_GetVar() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetVar("key1"))
  5. fmt.Println(m.GetVar("key2").IsNil())
  6. // Output:
  7. // val1
  8. // true
  9. }

GetVarOrSet

  • Description: GetVarOrSet queries and returns the key-value corresponding to the key name key. If the corresponding key-value does not exist, it uses value to set that key-value and returns the queried/settled key-value. The key-value is returned using the generic type *gvar.Var.

  • Format:

  1. GetVarOrSet(key interface{}, value interface{}) *gvar.Var
  • Example:
  1. func ExampleAnyAnyMap_GetVarOrSet() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
  5. fmt.Println(m.GetVarOrSet("key2", "val2"))
  6. // Output:
  7. // val1
  8. // val2
  9. }

GetVarOrSetFunc

  • Description: GetVarOrSetFunc queries and returns the key-value corresponding to the key name key. If the corresponding key-value does not exist, it uses the return value of func f to set that key-value and returns the queried/settled key-value. The key-value is returned using the generic type *gvar.Var.

  • Format:

  1. GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
  • Example:
  1. func ExampleAnyAnyMap_GetVarOrSetFunc() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
  5. return "NotExistValue"
  6. }))
  7. fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
  8. return "NotExistValue"
  9. }))
  10. // Output:
  11. // val1
  12. // NotExistValue
  13. }

GetVarOrSetFuncLock

  • Description: GetVarOrSetFuncLock queries and returns the key-value corresponding to the key name key. If the corresponding key-value does not exist, it uses the return value of func f to set that key-value and returns the queried/settled key-value. The key-value is returned using the generic type *gvar.Var.

  • Note: The difference between GetVarOrSetFuncLock and GetVarOrSetFunc is that it executes the function f in a write lock before execution when multiple goroutines call this method simultaneously.

  • Format:

  1. GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
  • Example:
  1. func ExampleAnyAnyMap_GetVarOrSetFuncLock() {
  2. m := gmap.New()
  3. m.Set("key1", "val1")
  4. fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
  5. return "NotExistValue"
  6. }))
  7. fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
  8. return "NotExistValue"
  9. }))
  10. // Output:
  11. // val1
  12. // NotExistValue
  13. }

SetIfNotExist

  • Description: If the key does not exist, SetIfNotExist sets the key-value pair key/value for the map and returns true. If the key exists, it returns false, and the value will be ignored.

  • Format:

  1. SetIfNotExist(key interface{}, value interface{}) bool
  • Example:
  1. func ExampleAnyAnyMap_SetIfNotExist() {
  2. var m gmap.Map
  3. fmt.Println(m.SetIfNotExist("k1", "v1"))
  4. fmt.Println(m.SetIfNotExist("k1", "v1"))
  5. fmt.Println(m.Map())
  6. // Output:
  7. // true
  8. // false
  9. // map[k1:v1]
  10. }

SetIfNotExistFunc

  • Description: If the key does not exist, SetIfNotExistFunc sets the value for the map to the return value of the function f and returns true. If the key exists, it returns false, and the value will be ignored.

  • Format:

  1. SetIfNotExistFunc(key interface{}, f func() interface{}) bool
  • Example:
  1. func ExampleAnyAnyMap_SetIfNotExistFunc() {
  2. var m gmap.Map
  3. fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
  4. return "v1"
  5. }))
  6. fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
  7. return "v1"
  8. }))
  9. fmt.Println(m.Map())
  10. // Output:
  11. // true
  12. // false
  13. // map[k1:v1]
  14. }

SetIfNotExistFuncLock

  • Description: If the key does not exist, SetIfNotExistFunc sets the value for the map to the return value of func f and then returns true. If the key exists, it returns false, and the value will be ignored.

  • Note: The difference between SetIfNotExistFuncLock and SetIfNotExistFunc is that it executes the function f in mutex.Lock.

  • Format:

  1. SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
  • Example:
  1. func ExampleAnyAnyMap_SetIfNotExistFuncLock() {
  2. var m gmap.Map
  3. fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
  4. return "v1"
  5. }))
  6. fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
  7. return "v1"
  8. }))
  9. fmt.Println(m.Map())
  10. // Output:
  11. // true
  12. // false
  13. // map[k1:v1]
  14. }

Remove

  • Description: Delete value from the map by the given key, and return the deleted value.

  • Format:

  1. Remove(key interface{}) (value interface{})
  • Example:
  1. func ExampleAnyAnyMap_Remove() {
  2. var m gmap.Map
  3. m.Set("k1", "v1")
  4. fmt.Println(m.Remove("k1"))
  5. fmt.Println(m.Remove("k2"))
  6. // Output:
  7. // v1
  8. // <nil>
  9. }

Removes

  • Description: Removes batch deletes the value of the map by the given key.

  • Format:

  1. Removes(keys []interface{})
  • Example:
  1. func ExampleAnyAnyMap_Removes() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. removeList := make([]interface{}, 2)
  10. removeList = append(removeList, "k1")
  11. removeList = append(removeList, "k2")
  12. m.Removes(removeList)
  13. fmt.Println(m.Map())
  14. // Output:
  15. // map[k3:v3 k4:v4]
  16. }

Keys

  • Description: Keys returns all the key of the map as slice.

  • Format:

  1. Keys() []interface{}
  • Example:
  1. func ExampleAnyAnyMap_Keys() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. fmt.Println(m.Keys())
  10. // Output:
  11. // [k1 k2 k3 k4]
  12. }

Values

  • Description: Values returns all the value of the map as slice.

  • Format:

  1. Values() []interface{}
  • Example:
  1. func ExampleAnyAnyMap_Values() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. fmt.Println(m.Values())
  10. // May Output:
  11. // [v1 v2 v3 v4]
  12. }

Contains

  • Description: Contains checks whether key exists. If key exists, it returns true, otherwise it returns false.

  • Note: The key name type is interface{}, so the match judgment needs to ensure that the type and value are identical.

  • Format:

  1. Contains(key interface{}) bool
  • Example:
  1. func ExampleAnyAnyMap_Contains() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. fmt.Println(m.Contains("k1"))
  10. fmt.Println(m.Contains("k5"))
  11. // Output:
  12. // true
  13. // false
  14. }

Size

  • Description: Size returns the size of the map.

  • Format:

  1. Size() int
  • Example:
  1. func ExampleAnyAnyMap_Size() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. fmt.Println(m.Size())
  10. // Output:
  11. // 4
  12. }

IsEmpty

  • Description: IsEmpty checks whether the map is empty. If the map is empty, it returns true, otherwise it returns false.

  • Format:

  1. IsEmpty() bool
  • Example:
  1. func ExampleAnyAnyMap_IsEmpty() {
  2. var m gmap.Map
  3. fmt.Println(m.IsEmpty())
  4. m.Set("k1", "v1")
  5. fmt.Println(m.IsEmpty())
  6. // Output:
  7. // true
  8. // false
  9. }

Clear

  • Description: Clear deletes all data of the map.

  • Format:

  1. Clear()
  • Example:
  1. func ExampleAnyAnyMap_Clear() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. m.Clear()
  10. fmt.Println(m.Map())
  11. // Output:
  12. // map[]
  13. }

Replace

  • Description: Replace completely replaces the map‘s value with the given data.

  • Format:

  1. Replace(data map[interface{}]interface{})
  • Example:
  1. func ExampleAnyAnyMap_Replace() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. })
  6. var n gmap.Map
  7. n.Sets(g.MapAnyAny{
  8. "k2": "v2",
  9. })
  10. fmt.Println(m.Map())
  11. m.Replace(n.Map())
  12. fmt.Println(m.Map())
  13. n.Set("k2", "v1")
  14. fmt.Println(m.Map())
  15. // Output:
  16. // map[k1:v1]
  17. // map[k2:v2]
  18. // map[k2:v1]
  19. }

LockFunc

  • Description: LockFunc executes the function f in the write lock.

  • Format:

  1. LockFunc(f func(m map[interface{}]interface{}))
  • Example:
  1. func ExampleAnyAnyMap_LockFunc() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": 1,
  5. "k2": 2,
  6. "k3": 3,
  7. "k4": 4,
  8. })
  9. m.LockFunc(func(m map[interface{}]interface{}) {
  10. totalValue := 0
  11. for _, v := range m {
  12. totalValue += v.(int)
  13. }
  14. fmt.Println("totalValue:", totalValue)
  15. })
  16. // Output:
  17. // totalValue: 10
  18. }

RLockFunc

  • Description: RLockFunc executes the function f in the read lock.

  • Format:

  1. RLockFunc(f func(m map[interface{}]interface{}))
  • Example:
  1. func ExampleAnyAnyMap_RLockFunc() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": 1,
  5. "k2": 2,
  6. "k3": 3,
  7. "k4": 4,
  8. })
  9. m.RLockFunc(func(m map[interface{}]interface{}) {
  10. totalValue := 0
  11. for _, v := range m {
  12. totalValue += v.(int)
  13. }
  14. fmt.Println("totalValue:", totalValue)
  15. })
  16. // Output:
  17. // totalValue: 10
  18. }

Flip

  • Description: Flip exchanges the key and value of the map.

  • Format:

  1. Flip()
  • Example:
  1. func ExampleAnyAnyMap_Flip() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. })
  6. m.Flip()
  7. fmt.Println(m.Map())
  8. // Output:
  9. // map[v1:k1]
  10. }

Merge

  • Description: Merge merges two AnyAnyMaps. The input parameter map will be merged into the original map.

  • Format:

  1. Merge(other *AnyAnyMap)
  • Example:
  1. func ExampleAnyAnyMap_Merge() {
  2. var m1, m2 gmap.Map
  3. m1.Set("key1", "val1")
  4. m2.Set("key2", "val2")
  5. m1.Merge(&m2)
  6. fmt.Println(m1.Map())
  7. // May Output:
  8. // map[key1:val1 key2:val2]
  9. }

String

  • Description: String returns the map in string form.

  • Format:

  1. String() string
  • Example:
  1. func ExampleAnyAnyMap_String() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. })
  6. fmt.Println(m.String())
  7. // Output:
  8. // {"k1":"v1"}
  9. }

MarshalJSON

  • Description: MarshalJSON implements the json.Marshal interface.

  • Format:

  1. MarshalJSON() ([]byte, error)
  • Example:
  1. func ExampleAnyAnyMap_MarshalJSON() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. bytes, err := m.MarshalJSON()
  10. if err == nil {
  11. fmt.Println(gconv.String(bytes))
  12. }
  13. // Output:
  14. // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
  15. }

UnmarshalJSON

  • Description: UnmarshalJSON implements the json.Unmarshal interface.

  • Format:

  1. UnmarshalJSON(b []byte) error
  • Example:
  1. func ExampleAnyAnyMap_UnmarshalJSON() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. var n gmap.Map
  10. err := n.UnmarshalJSON(gconv.Bytes(m.String()))
  11. if err == nil {
  12. fmt.Println(n.Map())
  13. }
  14. // Output:
  15. // map[k1:v1 k2:v2 k3:v3 k4:v4]
  16. }

UnmarshalValue

  • Description: UnmarshalValue is an interface implementation that initializes the current map through a variable of any type.

  • Format:

  1. UnmarshalValue(value interface{}) (err error)
  • Example:
  1. func ExampleAnyAnyMap_UnmarshalValue() {
  2. var m gmap.Map
  3. m.Sets(g.MapAnyAny{
  4. "k1": "v1",
  5. "k2": "v2",
  6. "k3": "v3",
  7. "k4": "v4",
  8. })
  9. var n gmap.Map
  10. err := n.UnmarshalValue(m.String())
  11. if err == nil {
  12. fmt.Println(n.Map())
  13. }
  14. // Output:
  15. // map[k1:v1 k2:v2 k3:v3 k4:v4]
  16. }