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 emptyAnyAnyMap
. The parametersafe
is used to specify whether to use a concurrency-safemap
, which isfalse
by default. -
Format:
New(safe ...bool) *Map
- Example:
func ExampleNew() {
m := gmap.New()
// Add data.
m.Set("key1", "val1")
// Print size.
fmt.Println(m.Size())
addMap := make(map[interface{}]interface{})
addMap["key2"] = "val2"
addMap["key3"] = "val3"
addMap[1] = 1
fmt.Println(m.Values())
// Batch add data.
m.Sets(addMap)
// Gets the value of the corresponding key.
fmt.Println(m.Get("key3"))
// Get the value by key, or set it with given key-value if not exist.
fmt.Println(m.GetOrSet("key4", "val4"))
// Set key-value if the key does not exist, then return true; or else return false.
fmt.Println(m.SetIfNotExist("key3", "val3"))
// Remove key
m.Remove("key2")
fmt.Println(m.Keys())
// Batch remove keys.
m.Removes([]interface{}{"key1", 1})
fmt.Println(m.Keys())
// Contains checks whether a key exists.
fmt.Println(m.Contains("key3"))
// Flip exchanges key-value of the map, it will change key-value to value-key.
m.Flip()
fmt.Println(m.Map())
// Clear deletes all data of the map.
m.Clear()
fmt.Println(m.Size())
// May Output:
// 1
// [val1]
// val3
// val4
// false
// [key4 key1 key3 1]
// [key4 key3]
// true
// map[val3:key3 val4:key4]
// 0
}
NewFrom
-
Description:
NewFrom
creates and returns anAnyAnyMap
with the data of the givenmap
. -
Note: The input parameter
map
will be set as the underlying data mapping (without deep copy), so external changes to themap
may introduce some safety issues simultaneously. Optional argumentsafe
specifies whether to use this structure in concurrency safety, default isfalse
. -
Format:
NewFrom(data map[interface{}]interface{}, safe ...bool) *Map
- Example:
func ExampleNewFrom() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m)
n := gmap.NewFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
Iterator
-
Description:
Iterator
iterates through thehashmap
in a read-only manner using a custom callback functionf
. Iff
returnstrue
, it continues iterating, if it returnsfalse
, it stops. -
Format:
Iterator(f func(k interface{}, v interface{}) bool)
- Example:
func ExampleAnyAnyMap_Iterator() {
m := gmap.New()
for i := 0; i < 10; i++ {
m.Set(i, i*2)
}
var totalKey, totalValue int
m.Iterator(func(k interface{}, v interface{}) bool {
totalKey += k.(int)
totalValue += v.(int)
return totalKey < 10
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
// May Output:
// totalKey: 11
// totalValue: 22
}
Clone
-
Description:
Clone
returns a newAnyAnyMap
, which contains a copy of the currentmap
data. -
Format:
Clone(safe ...bool) *AnyAnyMap
- Example:
func ExampleAnyAnyMap_Clone() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
Map
-
Description:
Map
returns the underlying datamap
. -
Note: If in concurrency safety, it returns a copy of the underlying data, otherwise it returns a pointer pointing to the underlying data.
-
Format:
Map() map[interface{}]interface{}
- Example:
func ExampleAnyAnyMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.New()
m1.Set("key1", "val1")
fmt.Println("m1:", m1)
n1 := m1.Map()
fmt.Println("before n1:", n1)
m1.Set("key1", "val2")
fmt.Println("after n1:", n1)
// concurrent-safety, copy of underlying data
m2 := gmap.New(true)
m2.Set("key1", "val1")
fmt.Println("m1:", m2)
n2 := m2.Map()
fmt.Println("before n2:", n2)
m2.Set("key1", "val2")
fmt.Println("after n2:", n2)
// Output:
// m1: {"key1":"val1"}
// before n1: map[key1:val1]
// after n1: map[key1:val2]
// m1: {"key1":"val1"}
// before n2: map[key1:val1]
// after n2: map[key1:val1]
}
MapCopy
-
Description:
MapCopy
returns a copy of the data in themap
. -
Format:
MapCopy() map[interface{}]interface{}
- Example:
func ExampleAnyAnyMap_MapCopy() {
m := gmap.New()
m.Set("key1", "val1")
m.Set("key2", "val2")
fmt.Println(m)
n := m.MapCopy()
fmt.Println(n)
// Output:
// {"key1":"val1","key2":"val2"}
// map[key1:val1 key2:val2]
}
MapStrAny
-
Description:
MapStrAny
returns a copy of themap
's data in the form ofmap[string]interface{}
. -
Format:
MapStrAny() map[string]interface{}
- Example:
func ExampleAnyAnyMap_MapStrAny() {
m := gmap.New()
m.Set(1001, "val1")
m.Set(1002, "val2")
n := m.MapStrAny()
fmt.Println(n)
// Output:
// map[1001:val1 1002:val2]
}
FilterEmpty
-
Description:
FilterEmpty
removes all key-value pairs with empty values. Values such as0
,nil
,false
,""
,len(slice/map/chan) == 0
are considered empty. -
Format:
FilterEmpty()
- Example:
func ExampleAnyAnyMap_FilterEmpty() {
m := gmap.NewFrom(g.MapAnyAny{
"k1": "",
"k2": nil,
"k3": 0,
"k4": 1,
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[k4:1]
}
FilterNil
-
Description:
FilterNil
removes all key-value pairs where the value isnil
. -
Format:
FilterNil()
- Example:
func ExampleAnyAnyMap_FilterNil() {
m := gmap.NewFrom(g.MapAnyAny{
"k1": "",
"k2": nil,
"k3": 0,
"k4": 1,
})
m.FilterNil()
fmt.Println(m.Map())
// May Output:
// map[k1: k3:0 k4:1]
}
Set
-
Description:
Set
sets thekey/value
for themap
. -
Format:
Set(key interface{}, value interface{})
- Example:
func ExampleAnyAnyMap_Set() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m)
// Output:
// {"key1":"val1"}
}
Sets
-
Description:
Sets
sets thekey/
value
batch for themap
. -
Format:
Sets(data map[interface{}]interface{})
- Example:
func ExampleAnyAnyMap_Sets() {
m := gmap.New()
addMap := make(map[interface{}]interface{})
addMap["key1"] = "val1"
addMap["key2"] = "val2"
addMap["key3"] = "val3"
m.Sets(addMap)
fmt.Println(m)
// Output:
// {"key1":"val1","key2":"val2","key3":"val3"}
}
Search
-
Description:
Search
searches themap
using the parameterkey
. If thekey
is found, it returns its corresponding value and the parameterfound
astrue
, otherwise asfalse
. -
Format:
Search(key interface{}) (value interface{}, found bool)
- Example:
func ExampleAnyAnyMap_Search() {
m := gmap.New()
m.Set("key1", "val1")
value, found := m.Search("key1")
if found {
fmt.Println("find key1 value:", value)
}
value, found = m.Search("key2")
if !found {
fmt.Println("key2 not find")
}
// Output:
// find key1 value: val1
// key2 not find
}
Get
-
Description:
Get
returns the value corresponding to the parameterkey
. If thekey
does not exist, it returnsNil
. -
Format:
Get(key interface{}) (value interface{})
- Example:
func ExampleAnyAnyMap_Get() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println("key1 value:", m.Get("key1"))
fmt.Println("key2 value:", m.Get("key2"))
// Output:
// key1 value: val1
// key2 value: <nil>
}
Pop
-
Description:
Pop
randomly retrieves and returns a key-value pair frommap
and deletes the key-value pair internally. -
Format:
Pop() (key, value interface{})
- Example:
func ExampleAnyAnyMap_Pop() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pop())
// May Output:
// k1 v1
}
Pops
-
Description:
Pops
randomly retrieves and deletessize
number of key-value pairs frommap
. Ifsize == -1
, it deletes and returns all key-value pairs. -
Format:
Pops(size int) map[interface{}]interface{}
- Example:
func ExampleAnyAnyMap_Pops() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pops(-1))
fmt.Println("size:", m.Size())
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pops(2))
fmt.Println("size:", m.Size())
// May Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
// size: 0
// map[k1:v1 k2:v2]
// size: 2
}
GetOrSet
-
Description:
GetOrSet
returnsvalue
ifkey
exists. Ifkey
does not exist, it sets the key-value to themap
and then returns the value. -
Format:
GetOrSet(key interface{}, value interface{}) interface{}
- Example:
func ExampleAnyAnyMap_GetOrSet() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetOrSet("key1", "NotExistValue"))
fmt.Println(m.GetOrSet("key2", "val2"))
// Output:
// val1
// val2
}
GetOrSetFunc
-
Description:
GetOrSetFunc
returnsvalue
ifkey
exists. Ifkey
does not exist, it sets the key-value formap
using the return value offunc f
and then returns the value. -
Format:
GetOrSetFunc(key interface{}, f func() interface{}) interface{}
- Example:
func ExampleAnyAnyMap_GetOrSetFunc() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
GetOrSetFuncLock
-
Description:
GetOrSetFunc
returnsvalue
ifkey
exists. Ifkey
does not exist, it sets the key-value formap
using the return value offunc f
and then returns the value. -
Note: The difference between
GetOrSetFuncLock
andGetOrSetFunc
is that it executes the functionf
in a write lock. -
Format:
GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}
- Example:
func ExampleAnyAnyMap_GetOrSetFuncLock() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
GetVar
-
Description:
GetVar
queries and returns the key-value corresponding to the key namekey
, with the key value returned using the generic type*gvar.Var
. -
Format:
GetVar(key interface{}) *gvar.Var
- Example:
func ExampleAnyAnyMap_GetVar() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetVar("key1"))
fmt.Println(m.GetVar("key2").IsNil())
// Output:
// val1
// true
}
GetVarOrSet
-
Description:
GetVarOrSet
queries and returns the key-value corresponding to the key namekey
. If the corresponding key-value does not exist, it usesvalue
to set that key-value and returns the queried/settled key-value. The key-value is returned using the generic type*gvar.Var
. -
Format:
GetVarOrSet(key interface{}, value interface{}) *gvar.Var
- Example:
func ExampleAnyAnyMap_GetVarOrSet() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
fmt.Println(m.GetVarOrSet("key2", "val2"))
// Output:
// val1
// val2
}
GetVarOrSetFunc
-
Description:
GetVarOrSetFunc
queries and returns the key-value corresponding to the key namekey
. If the corresponding key-value does not exist, it uses the return value offunc 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:
GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var
- Example:
func ExampleAnyAnyMap_GetVarOrSetFunc() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
GetVarOrSetFuncLock
-
Description:
GetVarOrSetFuncLock
queries and returns the key-value corresponding to the key namekey
. If the corresponding key-value does not exist, it uses the return value offunc 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
andGetVarOrSetFunc
is that it executes the functionf
in a write lock before execution when multiplegoroutine
s call this method simultaneously. -
Format:
GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var
- Example:
func ExampleAnyAnyMap_GetVarOrSetFuncLock() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
SetIfNotExist
-
Description: If the
key
does not exist,SetIfNotExist
sets the key-value pairkey/value
for themap
and returnstrue
. If thekey
exists, it returnsfalse
, and thevalue
will be ignored. -
Format:
SetIfNotExist(key interface{}, value interface{}) bool
- Example:
func ExampleAnyAnyMap_SetIfNotExist() {
var m gmap.Map
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
SetIfNotExistFunc
-
Description: If the
key
does not exist,SetIfNotExistFunc
sets the value for themap
to the return value of the functionf
and returnstrue
. If thekey
exists, it returnsfalse
, and thevalue
will be ignored. -
Format:
SetIfNotExistFunc(key interface{}, f func() interface{}) bool
- Example:
func ExampleAnyAnyMap_SetIfNotExistFunc() {
var m gmap.Map
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
SetIfNotExistFuncLock
-
Description: If the
key
does not exist,SetIfNotExistFunc
sets the value for themap
to the return value offunc f
and then returnstrue
. If thekey
exists, it returnsfalse
, and thevalue
will be ignored. -
Note: The difference between
SetIfNotExistFuncLock
andSetIfNotExistFunc
is that it executes the functionf
inmutex.Lock
. -
Format:
SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool
- Example:
func ExampleAnyAnyMap_SetIfNotExistFuncLock() {
var m gmap.Map
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
Remove
-
Description: Delete
value
from themap
by the givenkey
, and return the deletedvalue
. -
Format:
Remove(key interface{}) (value interface{})
- Example:
func ExampleAnyAnyMap_Remove() {
var m gmap.Map
m.Set("k1", "v1")
fmt.Println(m.Remove("k1"))
fmt.Println(m.Remove("k2"))
// Output:
// v1
// <nil>
}
Removes
-
Description:
Removes
batch deletes thevalue
of themap
by the givenkey
. -
Format:
Removes(keys []interface{})
- Example:
func ExampleAnyAnyMap_Removes() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
removeList := make([]interface{}, 2)
removeList = append(removeList, "k1")
removeList = append(removeList, "k2")
m.Removes(removeList)
fmt.Println(m.Map())
// Output:
// map[k3:v3 k4:v4]
}
Keys
-
Description:
Keys
returns all thekey
of themap
asslice
. -
Format:
Keys() []interface{}
- Example:
func ExampleAnyAnyMap_Keys() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
// Output:
// [k1 k2 k3 k4]
}
Values
-
Description:
Values
returns all thevalue
of themap
asslice
. -
Format:
Values() []interface{}
- Example:
func ExampleAnyAnyMap_Values() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Values())
// May Output:
// [v1 v2 v3 v4]
}
Contains
-
Description:
Contains
checks whetherkey
exists. Ifkey
exists, it returnstrue
, otherwise it returnsfalse
. -
Note: The key name type is
interface{}
, so the match judgment needs to ensure that the type and value are identical. -
Format:
Contains(key interface{}) bool
- Example:
func ExampleAnyAnyMap_Contains() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Contains("k1"))
fmt.Println(m.Contains("k5"))
// Output:
// true
// false
}
Size
-
Description:
Size
returns the size of themap
. -
Format:
Size() int
- Example:
func ExampleAnyAnyMap_Size() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Size())
// Output:
// 4
}
IsEmpty
-
Description:
IsEmpty
checks whether themap
is empty. If themap
is empty, it returnstrue
, otherwise it returnsfalse
. -
Format:
IsEmpty() bool
- Example:
func ExampleAnyAnyMap_IsEmpty() {
var m gmap.Map
fmt.Println(m.IsEmpty())
m.Set("k1", "v1")
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
Clear
-
Description:
Clear
deletes all data of themap
. -
Format:
Clear()
- Example:
func ExampleAnyAnyMap_Clear() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
Replace
-
Description:
Replace
completely replaces themap
'svalue
with the givendata
. -
Format:
Replace(data map[interface{}]interface{})
- Example:
func ExampleAnyAnyMap_Replace() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
})
var n gmap.Map
n.Sets(g.MapAnyAny{
"k2": "v2",
})
fmt.Println(m.Map())
m.Replace(n.Map())
fmt.Println(m.Map())
n.Set("k2", "v1")
fmt.Println(m.Map())
// Output:
// map[k1:v1]
// map[k2:v2]
// map[k2:v1]
}
LockFunc
-
Description:
LockFunc
executes the functionf
in the write lock. -
Format:
LockFunc(f func(m map[interface{}]interface{}))
- Example:
func ExampleAnyAnyMap_LockFunc() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.LockFunc(func(m map[interface{}]interface{}) {
totalValue := 0
for _, v := range m {
totalValue += v.(int)
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
RLockFunc
-
Description:
RLockFunc
executes the functionf
in the read lock. -
Format:
RLockFunc(f func(m map[interface{}]interface{}))
- Example:
func ExampleAnyAnyMap_RLockFunc() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.RLockFunc(func(m map[interface{}]interface{}) {
totalValue := 0
for _, v := range m {
totalValue += v.(int)
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
Flip
-
Description:
Flip
exchanges thekey
andvalue
of themap
. -
Format:
Flip()
- Example:
func ExampleAnyAnyMap_Flip() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[v1:k1]
}
Merge
-
Description:
Merge
merges two AnyAnyMaps. The input parametermap
will be merged into the originalmap
. -
Format:
Merge(other *AnyAnyMap)
- Example:
func ExampleAnyAnyMap_Merge() {
var m1, m2 gmap.Map
m1.Set("key1", "val1")
m2.Set("key2", "val2")
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:val1 key2:val2]
}
String
-
Description:
String
returns themap
in string form. -
Format:
String() string
- Example:
func ExampleAnyAnyMap_String() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
})
fmt.Println(m.String())
// Output:
// {"k1":"v1"}
}
MarshalJSON
-
Description:
MarshalJSON
implements thejson.Marshal
interface. -
Format:
MarshalJSON() ([]byte, error)
- Example:
func ExampleAnyAnyMap_MarshalJSON() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
bytes, err := m.MarshalJSON()
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
}
UnmarshalJSON
-
Description:
UnmarshalJSON
implements thejson.Unmarshal
interface. -
Format:
UnmarshalJSON(b []byte) error
- Example:
func ExampleAnyAnyMap_UnmarshalJSON() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
var n gmap.Map
err := n.UnmarshalJSON(gconv.Bytes(m.String()))
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
}
UnmarshalValue
-
Description:
UnmarshalValue
is an interface implementation that initializes the currentmap
through a variable of any type. -
Format:
UnmarshalValue(value interface{}) (err error)
- Example:
func ExampleAnyAnyMap_UnmarshalValue() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
var n gmap.Map
err := n.UnmarshalValue(m.String())
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
}