mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
Implemented AnyAnyMap Example Function:
1.Iterator 2.Clone 3.Map 4.MapCopy 5.MapStrAny 6.FilterEmpty 7.FilterNil 8.Set 9.Sets 10.Search 11.Get 12.Pop 13.Pops 14.GetOrSet 15.GetOrSetFunc 16.GetOrSetFuncLock 17.GetVar 18.GetVarOrSet 19.GetVarOrSetFunc 20.GetVarOrSetFuncLock
This commit is contained in:
@ -266,7 +266,7 @@ func (m *AnyAnyMap) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(m.Get(key))
|
||||
}
|
||||
|
||||
// GetVarOrSet returns a Var with result from GetVarOrSet.
|
||||
// GetVarOrSet returns a Var with result from GetOrSet.
|
||||
// The returned Var is un-concurrent safe.
|
||||
func (m *AnyAnyMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
|
||||
return gvar.New(m.GetOrSet(key, value))
|
||||
|
||||
@ -14,28 +14,96 @@ import (
|
||||
)
|
||||
|
||||
func ExampleAnyAnyMap_Iterator() {
|
||||
m := gmap.New()
|
||||
for i := 0; i < 10; i++ {
|
||||
m.Set(i, i*2)
|
||||
}
|
||||
|
||||
// Output:
|
||||
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
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Clone() {
|
||||
m := gmap.New()
|
||||
|
||||
m.Set("key1", "val1")
|
||||
fmt.Println(m)
|
||||
|
||||
n := m.Clone()
|
||||
fmt.Println(n)
|
||||
|
||||
// Output:
|
||||
// {"key1":"val1"}
|
||||
// {"key1":"val1"}
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_FilterEmpty() {
|
||||
@ -67,23 +135,61 @@ func ExampleAnyAnyMap_FilterNil() {
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Set() {
|
||||
m := gmap.New()
|
||||
|
||||
m.Set("key1", "val1")
|
||||
fmt.Println(m)
|
||||
|
||||
// Output:
|
||||
// {"key1":"val1"}
|
||||
}
|
||||
|
||||
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"}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Pop() {
|
||||
@ -94,14 +200,11 @@ func ExampleAnyAnyMap_Pop() {
|
||||
"k3": "v3",
|
||||
"k4": "v4",
|
||||
})
|
||||
|
||||
fmt.Println(m.Pop())
|
||||
fmt.Println(m.Pops(2))
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// May Output:
|
||||
// k1 v1
|
||||
// map[k2:v2 k4:v4]
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_Pops() {
|
||||
@ -112,49 +215,123 @@ func ExampleAnyAnyMap_Pops() {
|
||||
"k3": "v3",
|
||||
"k4": "v4",
|
||||
})
|
||||
fmt.Println(m.Pop())
|
||||
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(m.Size())
|
||||
fmt.Println("size:", m.Size())
|
||||
|
||||
// May Output:
|
||||
// k1 v1
|
||||
// map[k2:v2 k4:v4]
|
||||
// 1
|
||||
// map[k1:v1 k2:v2 k3:v3 k4:v4]
|
||||
// size: 0
|
||||
// map[k1:v1 k2:v2]
|
||||
// size: 2
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_GetVar() {
|
||||
m := gmap.New()
|
||||
m.Set("key1", "val1")
|
||||
|
||||
fmt.Println(m.GetVar("key1"))
|
||||
fmt.Println(m.GetVar("key2").IsNil())
|
||||
|
||||
// Output:
|
||||
// val1
|
||||
// true
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func ExampleAnyAnyMap_SetIfNotExist() {
|
||||
|
||||
Reference in New Issue
Block a user