Merge pull request #1489 from huangqian1985/master

gmap Example and gvalid Example
This commit is contained in:
John Guo 2021-11-23 20:40:33 +08:00 committed by GitHub
commit 28fe100761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 4664 additions and 130 deletions

View File

@ -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))

View File

@ -8,151 +8,104 @@ package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
)
func ExampleNew() {
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
}
func ExampleAnyAnyMap_Clone() {
m := gmap.New()
// Add data.
m.Set("key1", "val1")
fmt.Println(m)
// Print size.
fmt.Println(m.Size())
n := m.Clone()
fmt.Println(n)
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
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
func ExampleAnyAnyMap_Keys() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
fmt.Println(m.Values())
func ExampleAnyAnyMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.New()
m1.Set("key1", "val1")
fmt.Println("m1:", m1)
// May Output:
// [k1 k2 k3 k4]
// [v2 v3 v4 v1]
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("m2:", 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]
// m2: {"key1":"val1"}
// before n2: map[key1:val1]
// after n2: map[key1:val1]
}
func ExampleAnyAnyMap_Values() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
fmt.Println(m.Values())
func ExampleAnyAnyMap_MapCopy() {
m := gmap.New()
// May Output:
// [k1 k2 k3 k4]
// [v2 v3 v4 v1]
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_Flip() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
})
m.Flip()
fmt.Println(m.Map())
func ExampleAnyAnyMap_MapStrAny() {
m := gmap.New()
m.Set(1001, "val1")
m.Set(1002, "val2")
// May Output:
// map[v1:k1 v2:k2]
}
n := m.MapStrAny()
fmt.Printf("%#v", n)
func ExampleAnyAnyMap_Pop() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"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() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"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
// Output:
// map[string]interface {}{"1001":"val1", "1002":"val2"}
}
func ExampleAnyAnyMap_FilterEmpty() {
@ -165,7 +118,7 @@ func ExampleAnyAnyMap_FilterEmpty() {
m.FilterEmpty()
fmt.Println(m.Map())
// May Output:
// Output:
// map[k4:1]
}
@ -177,16 +130,216 @@ func ExampleAnyAnyMap_FilterNil() {
"k4": 1,
})
m.FilterNil()
fmt.Println(m.Map())
fmt.Printf("%#v", m.Map())
// Output:
// map[interface {}]interface {}{"k1":"", "k3":0, "k4":1}
}
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() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pop())
// May Output:
// map[k1: k3:0 k4:1]
// k1 v1
}
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
}
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() {
var m gmap.Map
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.SetIfNotExist("k1", "v2"))
fmt.Println(m.Map())
// Output:
@ -195,6 +348,241 @@ func ExampleAnyAnyMap_SetIfNotExist() {
// map[k1:v1]
}
func ExampleAnyAnyMap_SetIfNotExistFunc() {
var m gmap.Map
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleAnyAnyMap_SetIfNotExistFuncLock() {
var m gmap.Map
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleAnyAnyMap_Remove() {
var m gmap.Map
m.Set("k1", "v1")
fmt.Println(m.Remove("k1"))
fmt.Println(m.Remove("k2"))
fmt.Println(m.Size())
// Output:
// v1
// <nil>
// 0
}
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]
}
func ExampleAnyAnyMap_Keys() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
// May Output:
// [k1 k2 k3 k4]
}
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]
}
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
}
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
}
func ExampleAnyAnyMap_IsEmpty() {
var m gmap.Map
fmt.Println(m.IsEmpty())
m.Set("k1", "v1")
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
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[]
}
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]
}
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
}
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
}
func ExampleAnyAnyMap_Flip() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[v1:k1]
}
func ExampleAnyAnyMap_Merge() {
var m1, m2 gmap.Map
m1.Set("key1", "val1")
@ -205,3 +593,78 @@ func ExampleAnyAnyMap_Merge() {
// May Output:
// map[key1:val1 key2:val2]
}
func ExampleAnyAnyMap_String() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
})
fmt.Println(m.String())
// Output:
// {"k1":"v1"}
}
func ExampleAnyAnyMap_MarshalJSON() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
}
func ExampleAnyAnyMap_UnmarshalJSON() {
var m gmap.Map
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
var n gmap.Map
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
}
func ExampleAnyAnyMap_UnmarshalValue() {
type User struct {
Uid int
Name string
Pass1 string `gconv:"password1"`
Pass2 string `gconv:"password2"`
}
var (
m gmap.AnyAnyMap
user = User{
Uid: 1,
Name: "john",
Pass1: "123",
Pass2: "456",
}
)
if err := gconv.Scan(user, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}
}

View File

@ -0,0 +1,661 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
)
func ExampleIntAnyMap_Iterator() {
m := gmap.NewIntAnyMap()
for i := 0; i < 10; i++ {
m.Set(i, i*2)
}
var totalKey, totalValue int
m.Iterator(func(k int, v interface{}) bool {
totalKey += k
totalValue += v.(int)
return totalKey < 10
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
// May Output:
// totalKey: 11
// totalValue: 22
}
func ExampleIntAnyMap_Clone() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"1":"val1"}
// {"1":"val1"}
}
func ExampleIntAnyMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.NewIntAnyMap()
m1.Set(1, "val1")
fmt.Println("m1:", m1)
n1 := m1.Map()
fmt.Println("before n1:", n1)
m1.Set(1, "val2")
fmt.Println("after n1:", n1)
// concurrent-safety, copy of underlying data
m2 := gmap.New(true)
m2.Set(1, "val1")
fmt.Println("m2:", m2)
n2 := m2.Map()
fmt.Println("before n2:", n2)
m2.Set(1, "val2")
fmt.Println("after n2:", n2)
// Output:
// m1: {"1":"val1"}
// before n1: map[1:val1]
// after n1: map[1:val2]
// m2: {"1":"val1"}
// before n2: map[1:val1]
// after n2: map[1:val1]
}
func ExampleIntAnyMap_MapCopy() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
m.Set(2, "val2")
fmt.Println(m)
n := m.MapCopy()
fmt.Println(n)
// Output:
// {"1":"val1","2":"val2"}
// map[1:val1 2:val2]
}
func ExampleIntAnyMap_MapStrAny() {
m := gmap.NewIntAnyMap()
m.Set(1001, "val1")
m.Set(1002, "val2")
n := m.MapStrAny()
fmt.Printf("%#v", n)
// Output:
// map[string]interface {}{"1001":"val1", "1002":"val2"}
}
func ExampleIntAnyMap_FilterEmpty() {
m := gmap.NewIntAnyMapFrom(g.MapIntAny{
1: "",
2: nil,
3: 0,
4: 1,
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[4:1]
}
func ExampleIntAnyMap_FilterNil() {
m := gmap.NewIntAnyMapFrom(g.MapIntAny{
1: "",
2: nil,
3: 0,
4: 1,
})
m.FilterNil()
fmt.Printf("%#v", m.Map())
// Output:
// map[int]interface {}{1:"", 3:0, 4:1}
}
func ExampleIntAnyMap_Set() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m)
// Output:
// {"1":"val1"}
}
func ExampleIntAnyMap_Sets() {
m := gmap.NewIntAnyMap()
addMap := make(map[int]interface{})
addMap[1] = "val1"
addMap[2] = "val2"
addMap[3] = "val3"
m.Sets(addMap)
fmt.Println(m)
// Output:
// {"1":"val1","2":"val2","3":"val3"}
}
func ExampleIntAnyMap_Search() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
value, found := m.Search(1)
if found {
fmt.Println("find key1 value:", value)
}
value, found = m.Search(2)
if !found {
fmt.Println("key2 not find")
}
// Output:
// find key1 value: val1
// key2 not find
}
func ExampleIntAnyMap_Get() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println("key1 value:", m.Get(1))
fmt.Println("key2 value:", m.Get(2))
// Output:
// key1 value: val1
// key2 value: <nil>
}
func ExampleIntAnyMap_Pop() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Pop())
// May Output:
// 1 v1
}
func ExampleIntAnyMap_Pops() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Pops(-1))
fmt.Println("size:", m.Size())
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Pops(2))
fmt.Println("size:", m.Size())
// May Output:
// map[1:v1 2:v2 3:v3 4:v4]
// size: 0
// map[1:v1 2:v2]
// size: 2
}
func ExampleIntAnyMap_GetOrSet() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetOrSet(1, "NotExistValue"))
fmt.Println(m.GetOrSet(2, "val2"))
// Output:
// val1
// val2
}
func ExampleIntAnyMap_GetOrSetFunc() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetOrSetFunc(1, func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetOrSetFunc(2, func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
func ExampleIntAnyMap_GetOrSetFuncLock() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetOrSetFuncLock(1, func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetOrSetFuncLock(2, func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
func ExampleIntAnyMap_GetVar() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetVar(1))
fmt.Println(m.GetVar(2).IsNil())
// Output:
// val1
// true
}
func ExampleIntAnyMap_GetVarOrSet() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetVarOrSet(1, "NotExistValue"))
fmt.Println(m.GetVarOrSet(2, "val2"))
// Output:
// val1
// val2
}
func ExampleIntAnyMap_GetVarOrSetFunc() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetVarOrSetFunc(1, func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetVarOrSetFunc(2, func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
func ExampleIntAnyMap_GetVarOrSetFuncLock() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m.GetVarOrSetFuncLock(1, func() interface{} {
return "NotExistValue"
}))
fmt.Println(m.GetVarOrSetFuncLock(2, func() interface{} {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
func ExampleIntAnyMap_SetIfNotExist() {
var m gmap.IntAnyMap
fmt.Println(m.SetIfNotExist(1, "v1"))
fmt.Println(m.SetIfNotExist(1, "v2"))
fmt.Println(m.Map())
// Output:
// true
// false
// map[1:v1]
}
func ExampleIntAnyMap_SetIfNotExistFunc() {
var m gmap.IntAnyMap
fmt.Println(m.SetIfNotExistFunc(1, func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFunc(1, func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[1:v1]
}
func ExampleIntAnyMap_SetIfNotExistFuncLock() {
var m gmap.IntAnyMap
fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[1:v1]
}
func ExampleIntAnyMap_Remove() {
var m gmap.IntAnyMap
m.Set(1, "v1")
fmt.Println(m.Remove(1))
fmt.Println(m.Remove(2))
fmt.Println(m.Size())
// Output:
// v1
// <nil>
// 0
}
func ExampleIntAnyMap_Removes() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
removeList := make([]int, 2)
removeList = append(removeList, 1)
removeList = append(removeList, 2)
m.Removes(removeList)
fmt.Println(m.Map())
// Output:
// map[3:v3 4:v4]
}
func ExampleIntAnyMap_Keys() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Keys())
// May Output:
// [1 2 3 4]
}
func ExampleIntAnyMap_Values() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Values())
// May Output:
// [v1 v2 v3 v4]
}
func ExampleIntAnyMap_Contains() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Contains(1))
fmt.Println(m.Contains(5))
// Output:
// true
// false
}
func ExampleIntAnyMap_Size() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
fmt.Println(m.Size())
// Output:
// 4
}
func ExampleIntAnyMap_IsEmpty() {
var m gmap.IntAnyMap
fmt.Println(m.IsEmpty())
m.Set(1, "v1")
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
func ExampleIntAnyMap_Clear() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
func ExampleIntAnyMap_Replace() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
})
var n gmap.IntAnyMap
n.Sets(g.MapIntAny{
2: "v2",
})
fmt.Println(m.Map())
m.Replace(n.Map())
fmt.Println(m.Map())
n.Set(2, "v1")
fmt.Println(m.Map())
// Output:
// map[1:v1]
// map[2:v2]
// map[2:v1]
}
func ExampleIntAnyMap_LockFunc() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: 1,
2: 2,
3: 3,
4: 4,
})
m.LockFunc(func(m map[int]interface{}) {
totalValue := 0
for _, v := range m {
totalValue += v.(int)
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleIntAnyMap_RLockFunc() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: 1,
2: 2,
3: 3,
4: 4,
})
m.RLockFunc(func(m map[int]interface{}) {
totalValue := 0
for _, v := range m {
totalValue += v.(int)
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleIntAnyMap_Flip() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: 10,
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[10:1]
}
func ExampleIntAnyMap_Merge() {
var m1, m2 gmap.Map
m1.Set(1, "val1")
m2.Set(2, "val2")
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:val1 key2:val2]
}
func ExampleIntAnyMap_String() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
})
fmt.Println(m.String())
// Output:
// {"1":"v1"}
}
func ExampleIntAnyMap_MarshalJSON() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"1":"v1","2":"v2","3":"v3","4":"v4"}
}
func ExampleIntAnyMap_UnmarshalJSON() {
var m gmap.IntAnyMap
m.Sets(g.MapIntAny{
1: "v1",
2: "v2",
3: "v3",
4: "v4",
})
var n gmap.Map
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[1:v1 2:v2 3:v3 4:v4]
}
func ExampleIntAnyMap_UnmarshalValue() {
var m gmap.IntAnyMap
goWeb := map[int]interface{}{
1: "goframe",
2: "gin",
3: "echo",
}
if err := gconv.Scan(goWeb, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[int]interface {}{1:"goframe", 2:"gin", 3:"echo"}
}

View File

@ -0,0 +1,588 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
)
func ExampleIntIntMap_Iterator() {
m := gmap.NewIntIntMap()
for i := 0; i < 10; i++ {
m.Set(i, i*2)
}
var totalKey, totalValue int
m.Iterator(func(k int, v int) bool {
totalKey += k
totalValue += v
return totalKey < 10
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
// May Output:
// totalKey: 11
// totalValue: 22
}
func ExampleIntIntMap_Clone() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"1":1}
// {"1":1}
}
func ExampleIntIntMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.NewIntIntMap()
m1.Set(1, 1)
fmt.Println("m1:", m1)
n1 := m1.Map()
fmt.Println("before n1:", n1)
m1.Set(1, 2)
fmt.Println("after n1:", n1)
// concurrent-safety, copy of underlying data
m2 := gmap.New(true)
m2.Set(1, "1")
fmt.Println("m2:", m2)
n2 := m2.Map()
fmt.Println("before n2:", n2)
m2.Set(1, "2")
fmt.Println("after n2:", n2)
// Output:
// m1: {"1":1}
// before n1: map[1:1]
// after n1: map[1:2]
// m2: {"1":"1"}
// before n2: map[1:1]
// after n2: map[1:1]
}
func ExampleIntIntMap_MapCopy() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
m.Set(2, 2)
fmt.Println(m)
n := m.MapCopy()
fmt.Println(n)
// Output:
// {"1":1,"2":2}
// map[1:1 2:2]
}
func ExampleIntIntMap_MapStrAny() {
m := gmap.NewIntIntMap()
m.Set(1001, 1)
m.Set(1002, 2)
n := m.MapStrAny()
fmt.Printf("%#v", n)
// Output:
// map[string]interface {}{"1001":1, "1002":2}
}
func ExampleIntIntMap_FilterEmpty() {
m := gmap.NewIntIntMapFrom(g.MapIntInt{
1: 0,
2: 1,
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[2:1]
}
func ExampleIntIntMap_Set() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m)
// Output:
// {"1":1}
}
func ExampleIntIntMap_Sets() {
m := gmap.NewIntIntMap()
addMap := make(map[int]int)
addMap[1] = 1
addMap[2] = 12
addMap[3] = 123
m.Sets(addMap)
fmt.Println(m)
// Output:
// {"1":1,"2":12,"3":123}
}
func ExampleIntIntMap_Search() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
value, found := m.Search(1)
if found {
fmt.Println("find key1 value:", value)
}
value, found = m.Search(2)
if !found {
fmt.Println("key2 not find")
}
// Output:
// find key1 value: 1
// key2 not find
}
func ExampleIntIntMap_Get() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println("key1 value:", m.Get(1))
fmt.Println("key2 value:", m.Get(2))
// Output:
// key1 value: 1
// key2 value: 0
}
func ExampleIntIntMap_Pop() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Pop())
// May Output:
// 1 1
}
func ExampleIntIntMap_Pops() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Pops(-1))
fmt.Println("size:", m.Size())
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Pops(2))
fmt.Println("size:", m.Size())
// May Output:
// map[1:1 2:2 3:3 4:4]
// size: 0
// map[1:1 2:2]
// size: 2
}
func ExampleIntIntMap_GetOrSet() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m.GetOrSet(1, 0))
fmt.Println(m.GetOrSet(2, 2))
// Output:
// 1
// 2
}
func ExampleIntIntMap_GetOrSetFunc() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m.GetOrSetFunc(1, func() int {
return 0
}))
fmt.Println(m.GetOrSetFunc(2, func() int {
return 0
}))
// Output:
// 1
// 0
}
func ExampleIntIntMap_GetOrSetFuncLock() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m.GetOrSetFuncLock(1, func() int {
return 0
}))
fmt.Println(m.GetOrSetFuncLock(2, func() int {
return 0
}))
// Output:
// 1
// 0
}
func ExampleIntIntMap_SetIfNotExist() {
var m gmap.IntIntMap
fmt.Println(m.SetIfNotExist(1, 1))
fmt.Println(m.SetIfNotExist(1, 2))
fmt.Println(m.Map())
// Output:
// true
// false
// map[1:1]
}
func ExampleIntIntMap_SetIfNotExistFunc() {
var m gmap.IntIntMap
fmt.Println(m.SetIfNotExistFunc(1, func() int {
return 1
}))
fmt.Println(m.SetIfNotExistFunc(1, func() int {
return 2
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[1:1]
}
func ExampleIntIntMap_SetIfNotExistFuncLock() {
var m gmap.IntIntMap
fmt.Println(m.SetIfNotExistFuncLock(1, func() int {
return 1
}))
fmt.Println(m.SetIfNotExistFuncLock(1, func() int {
return 2
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[1:1]
}
func ExampleIntIntMap_Remove() {
var m gmap.IntIntMap
m.Set(1, 1)
fmt.Println(m.Remove(1))
fmt.Println(m.Remove(2))
fmt.Println(m.Size())
// Output:
// 1
// 0
// 0
}
func ExampleIntIntMap_Removes() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
removeList := make([]int, 2)
removeList = append(removeList, 1)
removeList = append(removeList, 2)
m.Removes(removeList)
fmt.Println(m.Map())
// Output:
// map[3:3 4:4]
}
func ExampleIntIntMap_Keys() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Keys())
// May Output:
// [1 2 3 4]
}
func ExampleIntIntMap_Values() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Values())
// May Output:
// [1 v2 v3 4]
}
func ExampleIntIntMap_Contains() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Contains(1))
fmt.Println(m.Contains(5))
// Output:
// true
// false
}
func ExampleIntIntMap_Size() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
fmt.Println(m.Size())
// Output:
// 4
}
func ExampleIntIntMap_IsEmpty() {
var m gmap.IntIntMap
fmt.Println(m.IsEmpty())
m.Set(1, 1)
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
func ExampleIntIntMap_Clear() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
func ExampleIntIntMap_Replace() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
})
var n gmap.IntIntMap
n.Sets(g.MapIntInt{
2: 2,
})
fmt.Println(m.Map())
m.Replace(n.Map())
fmt.Println(m.Map())
n.Set(2, 1)
fmt.Println(m.Map())
// Output:
// map[1:1]
// map[2:2]
// map[2:1]
}
func ExampleIntIntMap_LockFunc() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
m.LockFunc(func(m map[int]int) {
totalValue := 0
for _, v := range m {
totalValue += v
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleIntIntMap_RLockFunc() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
m.RLockFunc(func(m map[int]int) {
totalValue := 0
for _, v := range m {
totalValue += v
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleIntIntMap_Flip() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 10,
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[10:1]
}
func ExampleIntIntMap_Merge() {
var m1, m2 gmap.Map
m1.Set(1, "1")
m2.Set(2, "2")
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:1 key2:2]
}
func ExampleIntIntMap_String() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
})
fmt.Println(m.String())
// Output:
// {"1":1}
}
func ExampleIntIntMap_MarshalJSON() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"1":1,"2":2,"3":3,"4":4}
}
func ExampleIntIntMap_UnmarshalJSON() {
var m gmap.IntIntMap
m.Sets(g.MapIntInt{
1: 1,
2: 2,
3: 3,
4: 4,
})
var n gmap.Map
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[1:1 2:2 3:3 4:4]
}
func ExampleIntIntMap_UnmarshalValue() {
var m gmap.IntIntMap
n := map[int]int{
1: 1001,
2: 1002,
3: 1003,
}
if err := gconv.Scan(n, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[int]int{1:1001, 2:1002, 3:1003}
}

View File

@ -0,0 +1,624 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
)
func ExampleListMap_Iterator() {
m := gmap.NewListMap()
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)
// Output:
// totalKey: 10
// totalValue: 20
}
func ExampleListMap_IteratorAsc() {
m := gmap.NewListMap()
for i := 0; i < 10; i++ {
m.Set(i, i*2)
}
var totalKey, totalValue int
m.IteratorAsc(func(k interface{}, v interface{}) bool {
totalKey += k.(int)
totalValue += v.(int)
return totalKey < 10
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
// Output:
// totalKey: 10
// totalValue: 20
}
func ExampleListMap_IteratorDesc() {
m := gmap.NewListMap()
for i := 0; i < 10; i++ {
m.Set(i, i*2)
}
var totalKey, totalValue int
m.IteratorDesc(func(k interface{}, v interface{}) bool {
totalKey += k.(int)
totalValue += v.(int)
return totalKey < 10
})
fmt.Println("totalKey:", totalKey)
fmt.Println("totalValue:", totalValue)
// Output:
// totalKey: 17
// totalValue: 34
}
func ExampleListMap_Clone() {
m := gmap.NewListMap()
m.Set("key1", "val1")
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
func ExampleListMap_Clear() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
func ExampleListMap_Replace() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
})
var n gmap.ListMap
n.Sets(g.MapAnyAny{
"k2": "v2",
})
fmt.Println(m.Map())
m.Replace(n.Map())
fmt.Println(m.Map())
// Output:
// map[k1:v1]
// map[k2:v2]
}
func ExampleListMap_Map() {
m1 := gmap.NewListMap()
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)
// Output:
// m1: {"key1":"val1"}
// before n1: map[key1:val1]
// after n1: map[key1:val1]
}
func ExampleListMap_MapStrAny() {
m := gmap.NewListMap()
m.Set("key1", "val1")
m.Set("key2", "val2")
n := m.MapStrAny()
fmt.Printf("%#v", n)
// Output:
// map[string]interface {}{"key1":"val1", "key2":"val2"}
}
func ExampleListMap_FilterEmpty() {
m := gmap.NewListMapFrom(g.MapAnyAny{
"k1": "",
"k2": nil,
"k3": 0,
"k4": 1,
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[k4:1]
}
func ExampleListMap_Set() {
m := gmap.NewListMap()
m.Set("key1", "val1")
fmt.Println(m)
// Output:
// {"key1":"val1"}
}
func ExampleListMap_Sets() {
m := gmap.NewListMap()
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 ExampleListMap_Search() {
m := gmap.NewListMap()
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 ExampleListMap_Get() {
m := gmap.NewListMap()
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 ExampleListMap_Pop() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pop())
// May Output:
// k1 v1
}
func ExampleListMap_Pops() {
var m gmap.ListMap
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
}
func ExampleListMap_GetOrSet() {
m := gmap.NewListMap()
m.Set("key1", "val1")
fmt.Println(m.GetOrSet("key1", "NotExistValue"))
fmt.Println(m.GetOrSet("key2", "val2"))
// Output:
// val1
// val2
}
func ExampleListMap_GetOrSetFunc() {
m := gmap.NewListMap()
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 ExampleListMap_GetOrSetFuncLock() {
m := gmap.NewListMap()
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 ExampleListMap_GetVar() {
m := gmap.NewListMap()
m.Set("key1", "val1")
fmt.Println(m.GetVar("key1"))
fmt.Println(m.GetVar("key2").IsNil())
// Output:
// val1
// true
}
func ExampleListMap_GetVarOrSet() {
m := gmap.NewListMap()
m.Set("key1", "val1")
fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
fmt.Println(m.GetVarOrSet("key2", "val2"))
// Output:
// val1
// val2
}
func ExampleListMap_GetVarOrSetFunc() {
m := gmap.NewListMap()
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 ExampleListMap_GetVarOrSetFuncLock() {
m := gmap.NewListMap()
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 ExampleListMap_SetIfNotExist() {
var m gmap.ListMap
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.SetIfNotExist("k1", "v2"))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleListMap_SetIfNotExistFunc() {
var m gmap.ListMap
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleListMap_SetIfNotExistFuncLock() {
var m gmap.ListMap
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleListMap_Remove() {
var m gmap.ListMap
m.Set("k1", "v1")
fmt.Println(m.Remove("k1"))
fmt.Println(m.Remove("k2"))
fmt.Println(m.Size())
// Output:
// v1
// <nil>
// 0
}
func ExampleListMap_Removes() {
var m gmap.ListMap
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]
}
func ExampleListMap_Keys() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
// May Output:
// [k1 k2 k3 k4]
}
func ExampleListMap_Values() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Values())
// May Output:
// [v1 v2 v3 v4]
}
func ExampleListMap_Contains() {
var m gmap.ListMap
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
}
func ExampleListMap_Size() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Size())
// Output:
// 4
}
func ExampleListMap_IsEmpty() {
var m gmap.ListMap
fmt.Println(m.IsEmpty())
m.Set("k1", "v1")
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
func ExampleListMap_Flip() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[v1:k1]
}
func ExampleListMap_Merge() {
var m1, m2 gmap.ListMap
m1.Set("key1", "val1")
m2.Set("key2", "val2")
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:val1 key2:val2]
}
func ExampleListMap_String() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
})
fmt.Println(m.String())
// Output:
// {"k1":"v1"}
}
func ExampleListMap_MarshalJSON() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
}
func ExampleListMap_UnmarshalJSON() {
var m gmap.ListMap
m.Sets(g.MapAnyAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
var n gmap.ListMap
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
}
func ExampleListMap_UnmarshalValue() {
type User struct {
Uid int
Name string
Pass1 string `gconv:"password1"`
Pass2 string `gconv:"password2"`
}
var (
m gmap.AnyAnyMap
user = User{
Uid: 1,
Name: "john",
Pass1: "123",
Pass2: "456",
}
)
if err := gconv.Scan(user, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}
}

View File

@ -0,0 +1,658 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
)
func ExampleStrAnyMap_Iterator() {
m := gmap.NewStrAnyMap()
for i := 1; i <= 10; i++ {
m.Set(gconv.String(i), i*2)
}
var totalValue int
m.Iterator(func(k string, v interface{}) bool {
totalValue += v.(int)
return totalValue < 50
})
fmt.Println("totalValue:", totalValue)
// May Output:
// totalValue: 52
}
func ExampleStrAnyMap_Clone() {
m := gmap.NewStrAnyMap()
m.Set("key1", "val1")
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
func ExampleStrAnyMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.NewStrAnyMap()
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.NewStrAnyMap(true)
m2.Set("key1", "val1")
fmt.Println("m2:", 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]
// m2: {"key1":"val1"}
// before n2: map[key1:val1]
// after n2: map[key1:val1]
}
func ExampleStrAnyMap_MapCopy() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_MapStrAny() {
m := gmap.NewStrAnyMap()
m.Set("key1", "val1")
m.Set("key2", "val2")
n := m.MapStrAny()
fmt.Printf("%#v", n)
// Output:
// map[string]interface {}{"key1":"val1", "key2":"val2"}
}
func ExampleStrAnyMap_FilterEmpty() {
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
"k1": "",
"k2": nil,
"k3": 0,
"k4": 1,
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[k4:1]
}
func ExampleStrAnyMap_FilterNil() {
m := gmap.NewStrAnyMapFrom(g.MapStrAny{
"k1": "",
"k2": nil,
"k3": 0,
"k4": 1,
})
m.FilterNil()
fmt.Printf("%#v", m.Map())
// Output:
// map[string]interface {}{"k1":"", "k3":0, "k4":1}
}
func ExampleStrAnyMap_Set() {
m := gmap.NewStrAnyMap()
m.Set("key1", "val1")
fmt.Println(m)
// Output:
// {"key1":"val1"}
}
func ExampleStrAnyMap_Sets() {
m := gmap.NewStrAnyMap()
addMap := make(map[string]interface{})
addMap["key1"] = "val1"
addMap["key2"] = "val2"
addMap["key3"] = "val3"
m.Sets(addMap)
fmt.Println(m)
// Output:
// {"key1":"val1","key2":"val2","key3":"val3"}
}
func ExampleStrAnyMap_Search() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_Get() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_Pop() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pop())
// May Output:
// k1 v1
}
func ExampleStrAnyMap_Pops() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pops(-1))
fmt.Println("size:", m.Size())
m.Sets(g.MapStrAny{
"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
}
func ExampleStrAnyMap_GetOrSet() {
m := gmap.NewStrAnyMap()
m.Set("key1", "val1")
fmt.Println(m.GetOrSet("key1", "NotExistValue"))
fmt.Println(m.GetOrSet("key2", "val2"))
// Output:
// val1
// val2
}
func ExampleStrAnyMap_GetOrSetFunc() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_GetOrSetFuncLock() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_GetVar() {
m := gmap.NewStrAnyMap()
m.Set("key1", "val1")
fmt.Println(m.GetVar("key1"))
fmt.Println(m.GetVar("key2").IsNil())
// Output:
// val1
// true
}
func ExampleStrAnyMap_GetVarOrSet() {
m := gmap.NewStrAnyMap()
m.Set("key1", "val1")
fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
fmt.Println(m.GetVarOrSet("key2", "val2"))
// Output:
// val1
// val2
}
func ExampleStrAnyMap_GetVarOrSetFunc() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_GetVarOrSetFuncLock() {
m := gmap.NewStrAnyMap()
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 ExampleStrAnyMap_SetIfNotExist() {
var m gmap.StrAnyMap
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.SetIfNotExist("k1", "v2"))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleStrAnyMap_SetIfNotExistFunc() {
var m gmap.StrAnyMap
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleStrAnyMap_SetIfNotExistFuncLock() {
var m gmap.StrAnyMap
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v1"
}))
fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleStrAnyMap_Remove() {
var m gmap.StrAnyMap
m.Set("k1", "v1")
fmt.Println(m.Remove("k1"))
fmt.Println(m.Remove("k2"))
fmt.Println(m.Size())
// Output:
// v1
// <nil>
// 0
}
func ExampleStrAnyMap_Removes() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
removeList := make([]string, 2)
removeList = append(removeList, "k1")
removeList = append(removeList, "k2")
m.Removes(removeList)
fmt.Println(m.Map())
// Output:
// map[k3:v3 k4:v4]
}
func ExampleStrAnyMap_Keys() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
// May Output:
// [k1 k2 k3 k4]
}
func ExampleStrAnyMap_Values() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Values())
// May Output:
// [v1 v2 v3 v4]
}
func ExampleStrAnyMap_Contains() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Contains("k1"))
fmt.Println(m.Contains("k5"))
// Output:
// true
// false
}
func ExampleStrAnyMap_Size() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Size())
// Output:
// 4
}
func ExampleStrAnyMap_IsEmpty() {
var m gmap.StrAnyMap
fmt.Println(m.IsEmpty())
m.Set("k1", "v1")
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
func ExampleStrAnyMap_Clear() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
func ExampleStrAnyMap_Replace() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
})
var n gmap.StrAnyMap
n.Sets(g.MapStrAny{
"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]
}
func ExampleStrAnyMap_LockFunc() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.LockFunc(func(m map[string]interface{}) {
totalValue := 0
for _, v := range m {
totalValue += v.(int)
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleStrAnyMap_RLockFunc() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.RLockFunc(func(m map[string]interface{}) {
totalValue := 0
for _, v := range m {
totalValue += v.(int)
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleStrAnyMap_Flip() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[v1:k1]
}
func ExampleStrAnyMap_Merge() {
var m1, m2 gmap.StrAnyMap
m1.Set("key1", "val1")
m2.Set("key2", "val2")
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:val1 key2:val2]
}
func ExampleStrAnyMap_String() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
})
fmt.Println(m.String())
// Output:
// {"k1":"v1"}
}
func ExampleStrAnyMap_MarshalJSON() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
}
func ExampleStrAnyMap_UnmarshalJSON() {
var m gmap.StrAnyMap
m.Sets(g.MapStrAny{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
var n gmap.StrAnyMap
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
}
func ExampleStrAnyMap_UnmarshalValue() {
var m gmap.StrAnyMap
goWeb := map[string]interface{}{
"goframe": "https://goframe.org",
"gin": "https://gin-gonic.com/",
"echo": "https://echo.labstack.com/",
}
if err := gconv.Scan(goWeb, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[string]interface {}{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"}
}

View File

@ -0,0 +1,594 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/container/gmap"
)
func ExampleStrIntMap_Iterator() {
m := gmap.NewStrIntMap()
for i := 0; i < 10; i++ {
m.Set(gconv.String(i), i*2)
}
var totalValue int
m.Iterator(func(k string, v int) bool {
totalValue += v
return totalValue < 50
})
fmt.Println("totalValue:", totalValue)
// May Output:
// totalValue: 52
}
func ExampleStrIntMap_Clone() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"key1":1}
// {"key1":1}
}
func ExampleStrIntMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.NewStrIntMap()
m1.Set("key1", 1)
fmt.Println("m1:", m1)
n1 := m1.Map()
fmt.Println("before n1:", n1)
m1.Set("key1", 2)
fmt.Println("after n1:", n1)
// concurrent-safety, copy of underlying data
m2 := gmap.NewStrIntMap(true)
m2.Set("key1", 1)
fmt.Println("m2:", m2)
n2 := m2.Map()
fmt.Println("before n2:", n2)
m2.Set("key1", 2)
fmt.Println("after n2:", n2)
// Output:
// m1: {"key1":1}
// before n1: map[key1:1]
// after n1: map[key1:2]
// m2: {"key1":1}
// before n2: map[key1:1]
// after n2: map[key1:1]
}
func ExampleStrIntMap_MapCopy() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
m.Set("key2", 2)
fmt.Println(m)
n := m.MapCopy()
fmt.Println(n)
// Output:
// {"key1":1,"key2":2}
// map[key1:1 key2:2]
}
func ExampleStrIntMap_MapStrAny() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
m.Set("key2", 2)
n := m.MapStrAny()
fmt.Printf("%#v", n)
// Output:
// map[string]interface {}{"key1":1, "key2":2}
}
func ExampleStrIntMap_FilterEmpty() {
m := gmap.NewStrIntMapFrom(g.MapStrInt{
"k1": 0,
"k2": 1,
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[k2:1]
}
func ExampleStrIntMap_Set() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m)
// Output:
// {"key1":1}
}
func ExampleStrIntMap_Sets() {
m := gmap.NewStrIntMap()
addMap := make(map[string]int)
addMap["key1"] = 1
addMap["key2"] = 2
addMap["key3"] = 3
m.Sets(addMap)
fmt.Println(m)
// Output:
// {"key1":1,"key2":2,"key3":3}
}
func ExampleStrIntMap_Search() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
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: 1
// key2 not find
}
func ExampleStrIntMap_Get() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println("key1 value:", m.Get("key1"))
fmt.Println("key2 value:", m.Get("key2"))
// Output:
// key1 value: 1
// key2 value: 0
}
func ExampleStrIntMap_Pop() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Pop())
// May Output:
// k1 1
}
func ExampleStrIntMap_Pops() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Pops(-1))
fmt.Println("size:", m.Size())
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Pops(2))
fmt.Println("size:", m.Size())
// May Output:
// map[k1:1 k2:2 k3:3 k4:4]
// size: 0
// map[k1:1 k2:2]
// size: 2
}
func ExampleStrIntMap_GetOrSet() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m.GetOrSet("key1", 0))
fmt.Println(m.GetOrSet("key2", 2))
// Output:
// 1
// 2
}
func ExampleStrIntMap_GetOrSetFunc() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m.GetOrSetFunc("key1", func() int {
return 0
}))
fmt.Println(m.GetOrSetFunc("key2", func() int {
return 0
}))
// Output:
// 1
// 0
}
func ExampleStrIntMap_GetOrSetFuncLock() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m.GetOrSetFuncLock("key1", func() int {
return 0
}))
fmt.Println(m.GetOrSetFuncLock("key2", func() int {
return 0
}))
// Output:
// 1
// 0
}
func ExampleStrIntMap_SetIfNotExist() {
var m gmap.StrIntMap
fmt.Println(m.SetIfNotExist("k1", 1))
fmt.Println(m.SetIfNotExist("k1", 2))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:1]
}
func ExampleStrIntMap_SetIfNotExistFunc() {
var m gmap.StrIntMap
fmt.Println(m.SetIfNotExistFunc("k1", func() int {
return 1
}))
fmt.Println(m.SetIfNotExistFunc("k1", func() int {
return 2
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:1]
}
func ExampleStrIntMap_SetIfNotExistFuncLock() {
var m gmap.StrIntMap
fmt.Println(m.SetIfNotExistFuncLock("k1", func() int {
return 1
}))
fmt.Println(m.SetIfNotExistFuncLock("k1", func() int {
return 2
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:1]
}
func ExampleStrIntMap_Remove() {
var m gmap.StrIntMap
m.Set("k1", 1)
fmt.Println(m.Remove("k1"))
fmt.Println(m.Remove("k2"))
fmt.Println(m.Size())
// Output:
// 1
// 0
// 0
}
func ExampleStrIntMap_Removes() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
removeList := make([]string, 2)
removeList = append(removeList, "k1")
removeList = append(removeList, "k2")
m.Removes(removeList)
fmt.Println(m.Map())
// Output:
// map[k3:3 k4:4]
}
func ExampleStrIntMap_Keys() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Keys())
// May Output:
// [k1 k2 k3 k4]
}
func ExampleStrIntMap_Values() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Values())
// May Output:
// [1 2 3 4]
}
func ExampleStrIntMap_Contains() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Contains("k1"))
fmt.Println(m.Contains("k5"))
// Output:
// true
// false
}
func ExampleStrIntMap_Size() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
fmt.Println(m.Size())
// Output:
// 4
}
func ExampleStrIntMap_IsEmpty() {
var m gmap.StrIntMap
fmt.Println(m.IsEmpty())
m.Set("k1", 1)
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
func ExampleStrIntMap_Clear() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
func ExampleStrIntMap_Replace() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
})
var n gmap.StrIntMap
n.Sets(g.MapStrInt{
"k2": 2,
})
fmt.Println(m.Map())
m.Replace(n.Map())
fmt.Println(m.Map())
n.Set("k2", 1)
fmt.Println(m.Map())
// Output:
// map[k1:1]
// map[k2:2]
// map[k2:1]
}
func ExampleStrIntMap_LockFunc() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.LockFunc(func(m map[string]int) {
totalValue := 0
for _, v := range m {
totalValue += v
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleStrIntMap_RLockFunc() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
m.RLockFunc(func(m map[string]int) {
totalValue := 0
for _, v := range m {
totalValue += v
}
fmt.Println("totalValue:", totalValue)
})
// Output:
// totalValue: 10
}
func ExampleStrIntMap_Flip() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
})
m.Flip()
fmt.Println(m.Map())
var n gmap.StrIntMap
n.Sets(g.MapStrInt{
"11": 1,
})
n.Flip()
fmt.Println(n.Map())
// Output:
// map[1:0]
// map[1:11]
}
func ExampleStrIntMap_Merge() {
var m1, m2 gmap.StrIntMap
m1.Set("key1", 1)
m2.Set("key2", 2)
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:1 key2:2]
}
func ExampleStrIntMap_String() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
})
fmt.Println(m.String())
// Output:
// {"k1":1}
}
func ExampleStrIntMap_MarshalJSON() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"k1":1,"k2":2,"k3":3,"k4":4}
}
func ExampleStrIntMap_UnmarshalJSON() {
var m gmap.StrIntMap
m.Sets(g.MapStrInt{
"k1": 1,
"k2": 2,
"k3": 3,
"k4": 4,
})
var n gmap.StrIntMap
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:1 k2:2 k3:3 k4:4]
}
func ExampleStrIntMap_UnmarshalValue() {
var m gmap.StrIntMap
goWeb := map[string]int{
"goframe": 1,
"gin": 2,
"echo": 3,
}
if err := gconv.Scan(goWeb, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[string]int{"echo":3, "gin":2, "goframe":1}
}

View File

@ -0,0 +1,590 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/container/gmap"
)
func ExampleStrStrMap_Iterator() {
m := gmap.NewStrStrMap()
for i := 0; i < 10; i++ {
m.Set("key"+gconv.String(i), "var"+gconv.String(i))
}
var str string
m.Iterator(func(k string, v string) bool {
str += v + "|"
return len(str) < 20
})
fmt.Println("str:", str)
// May Output:
// var0|var1|var2|var3|
}
func ExampleStrStrMap_Clone() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
fmt.Println(m)
n := m.Clone()
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
func ExampleStrStrMap_Map() {
// non concurrent-safety, a pointer to the underlying data
m1 := gmap.NewStrStrMap()
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.NewStrStrMap(true)
m2.Set("key1", "val1")
fmt.Println("m2:", 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]
// m2: {"key1":"val1"}
// before n2: map[key1:val1]
// after n2: map[key1:val1]
}
func ExampleStrStrMap_MapCopy() {
m := gmap.NewStrStrMap()
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 ExampleStrStrMap_MapStrAny() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
m.Set("key2", "val2")
n := m.MapStrAny()
fmt.Printf("%#v", n)
// Output:
// map[string]interface {}{"key1":"val1", "key2":"val2"}
}
func ExampleStrStrMap_FilterEmpty() {
m := gmap.NewStrStrMapFrom(g.MapStrStr{
"k1": "",
"k2": "v2",
})
m.FilterEmpty()
fmt.Println(m.Map())
// Output:
// map[k2:v2]
}
func ExampleStrStrMap_Set() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
fmt.Println(m)
// Output:
// {"key1":"val1"}
}
func ExampleStrStrMap_Sets() {
m := gmap.NewStrStrMap()
addMap := make(map[string]string)
addMap["key1"] = "val1"
addMap["key2"] = "val2"
addMap["key3"] = "val3"
m.Sets(addMap)
fmt.Println(m)
// Output:
// {"key1":"val1","key2":"val2","key3":"val3"}
}
func ExampleStrStrMap_Search() {
m := gmap.NewStrStrMap()
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 ExampleStrStrMap_Get() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
fmt.Println("key1 value:", m.Get("key1"))
fmt.Println("key2 value:", m.Get("key2"))
// Output:
// key1 value: val1
// key2 value:
}
func ExampleStrStrMap_Pop() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pop())
// May Output:
// k1 v1
}
func ExampleStrStrMap_Pops() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Pops(-1))
fmt.Println("size:", m.Size())
m.Sets(g.MapStrStr{
"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
}
func ExampleStrStrMap_GetOrSet() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
fmt.Println(m.GetOrSet("key1", "NotExistValue"))
fmt.Println(m.GetOrSet("key2", "val2"))
// Output:
// val1
// val2
}
func ExampleStrStrMap_GetOrSetFunc() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
fmt.Println(m.GetOrSetFunc("key1", func() string {
return "NotExistValue"
}))
fmt.Println(m.GetOrSetFunc("key2", func() string {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
func ExampleStrStrMap_GetOrSetFuncLock() {
m := gmap.NewStrStrMap()
m.Set("key1", "val1")
fmt.Println(m.GetOrSetFuncLock("key1", func() string {
return "NotExistValue"
}))
fmt.Println(m.GetOrSetFuncLock("key2", func() string {
return "NotExistValue"
}))
// Output:
// val1
// NotExistValue
}
func ExampleStrStrMap_SetIfNotExist() {
var m gmap.StrStrMap
fmt.Println(m.SetIfNotExist("k1", "v1"))
fmt.Println(m.SetIfNotExist("k1", "v2"))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleStrStrMap_SetIfNotExistFunc() {
var m gmap.StrStrMap
fmt.Println(m.SetIfNotExistFunc("k1", func() string {
return "v1"
}))
fmt.Println(m.SetIfNotExistFunc("k1", func() string {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleStrStrMap_SetIfNotExistFuncLock() {
var m gmap.StrStrMap
fmt.Println(m.SetIfNotExistFuncLock("k1", func() string {
return "v1"
}))
fmt.Println(m.SetIfNotExistFuncLock("k1", func() string {
return "v2"
}))
fmt.Println(m.Map())
// Output:
// true
// false
// map[k1:v1]
}
func ExampleStrStrMap_Remove() {
var m gmap.StrStrMap
m.Set("k1", "v1")
fmt.Println(m.Remove("k1"))
fmt.Println(len(m.Remove("k2")))
fmt.Println(m.Size())
// Output:
// v1
// 0
// 0
}
func ExampleStrStrMap_Removes() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
removeList := make([]string, 2)
removeList = append(removeList, "k1")
removeList = append(removeList, "k2")
m.Removes(removeList)
fmt.Println(m.Map())
// Output:
// map[k3:v3 k4:v4]
}
func ExampleStrStrMap_Keys() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Keys())
// May Output:
// [k1 k2 k3 k4]
}
func ExampleStrStrMap_Values() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Values())
// May Output:
// [v1 v2 v3 v4]
}
func ExampleStrStrMap_Contains() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Contains("k1"))
fmt.Println(m.Contains("k5"))
// Output:
// true
// false
}
func ExampleStrStrMap_Size() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
fmt.Println(m.Size())
// Output:
// 4
}
func ExampleStrStrMap_IsEmpty() {
var m gmap.StrStrMap
fmt.Println(m.IsEmpty())
m.Set("k1", "v1")
fmt.Println(m.IsEmpty())
// Output:
// true
// false
}
func ExampleStrStrMap_Clear() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
m.Clear()
fmt.Println(m.Map())
// Output:
// map[]
}
func ExampleStrStrMap_Replace() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
})
var n gmap.StrStrMap
n.Sets(g.MapStrStr{
"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]
}
func ExampleStrStrMap_LockFunc() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
m.LockFunc(func(m map[string]string) {
for k, v := range m {
fmt.Println("key:", k, " value:", v)
}
})
// May Output:
// key: k1 value: v1
// key: k2 value: v2
// key: k3 value: v3
// key: k4 value: v4
}
func ExampleStrStrMap_RLockFunc() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
m.RLockFunc(func(m map[string]string) {
for k, v := range m {
fmt.Println("key:", k, " value:", v)
}
})
// May Output:
// key: k1 value: v1
// key: k2 value: v2
// key: k3 value: v3
// key: k4 value: v4
}
func ExampleStrStrMap_Flip() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
})
m.Flip()
fmt.Println(m.Map())
// Output:
// map[v1:k1]
}
func ExampleStrStrMap_Merge() {
var m1, m2 gmap.StrStrMap
m1.Set("key1", "val1")
m2.Set("key2", "val2")
m1.Merge(&m2)
fmt.Println(m1.Map())
// May Output:
// map[key1:val1 key2:val2]
}
func ExampleStrStrMap_String() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
})
fmt.Println(m.String())
// Output:
// {"k1":"v1"}
}
func ExampleStrStrMap_MarshalJSON() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
bytes, err := json.Marshal(&m)
if err == nil {
fmt.Println(gconv.String(bytes))
}
// Output:
// {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
}
func ExampleStrStrMap_UnmarshalJSON() {
var m gmap.StrStrMap
m.Sets(g.MapStrStr{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
})
var n gmap.StrStrMap
err := json.Unmarshal(gconv.Bytes(m.String()), &n)
if err == nil {
fmt.Println(n.Map())
}
// Output:
// map[k1:v1 k2:v2 k3:v3 k4:v4]
}
func ExampleStrStrMap_UnmarshalValue() {
var m gmap.StrStrMap
goWeb := map[string]string{
"goframe": "https://goframe.org",
"gin": "https://gin-gonic.com/",
"echo": "https://echo.labstack.com/",
}
if err := gconv.Scan(goWeb, &m); err == nil {
fmt.Printf("%#v", m.Map())
}
// Output:
// map[string]string{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"}
}

View File

@ -0,0 +1,311 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with gm file,
// You can obtain one at https://github.com/gogf/gf.
package gmap_test
import (
"fmt"
"github.com/gogf/gf/v2/util/gutil"
"github.com/gogf/gf/v2/container/gmap"
)
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
}
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"}
}
func ExampleNewHashMap() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m)
// Output:
// {"key1":"val1"}
}
func ExampleNewHashMapFrom() {
m := gmap.New()
m.Set("key1", "val1")
fmt.Println(m)
n := gmap.NewFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
func ExampleNewAnyAnyMap() {
m := gmap.NewAnyAnyMap()
m.Set("key1", "val1")
fmt.Println(m)
// Output:
// {"key1":"val1"}
}
func ExampleNewAnyAnyMapFrom() {
m := gmap.NewAnyAnyMap()
m.Set("key1", "val1")
fmt.Println(m)
n := gmap.NewAnyAnyMapFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"key1":"val1"}
// {"key1":"val1"}
}
func ExampleNewIntAnyMap() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m)
// Output:
// {"1":"val1"}
}
func ExampleNewIntAnyMapFrom() {
m := gmap.NewIntAnyMap()
m.Set(1, "val1")
fmt.Println(m)
n := gmap.NewIntAnyMapFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"1":"val1"}
// {"1":"val1"}
}
func ExampleNewIntIntMap() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m)
// Output:
// {"1":1}
}
func ExampleNewIntIntMapFrom() {
m := gmap.NewIntIntMap()
m.Set(1, 1)
fmt.Println(m)
n := gmap.NewIntIntMapFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"1":1}
// {"1":1}
}
func ExampleNewStrAnyMap() {
m := gmap.NewStrAnyMap()
m.Set("key1", "var1")
fmt.Println(m)
// Output:
// {"key1":"var1"}
}
func ExampleNewStrAnyMapFrom() {
m := gmap.NewStrAnyMap()
m.Set("key1", "var1")
fmt.Println(m)
n := gmap.NewStrAnyMapFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"key1":"var1"}
// {"key1":"var1"}
}
func ExampleNewStrIntMap() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m)
// Output:
// {"key1":1}
}
func ExampleNewStrIntMapFrom() {
m := gmap.NewStrIntMap()
m.Set("key1", 1)
fmt.Println(m)
n := gmap.NewStrIntMapFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"key1":1}
// {"key1":1}
}
func ExampleNewStrStrMap() {
m := gmap.NewStrStrMap()
m.Set("key1", "var1")
fmt.Println(m)
// Output:
// {"key1":"var1"}
}
func ExampleNewStrStrMapFrom() {
m := gmap.NewStrStrMap()
m.Set("key1", "var1")
fmt.Println(m)
n := gmap.NewStrStrMapFrom(m.MapCopy(), true)
fmt.Println(n)
// Output:
// {"key1":"var1"}
// {"key1":"var1"}
}
func ExampleNewListMap() {
m := gmap.NewListMap()
m.Set("key1", "var1")
m.Set("key2", "var2")
fmt.Println(m)
// Output:
// {"key1":"var1","key2":"var2"}
}
func ExampleNewListMapFrom() {
m := gmap.NewListMap()
m.Set("key1", "var1")
m.Set("key2", "var2")
fmt.Println(m)
n := gmap.NewListMapFrom(m.Map(), true)
fmt.Println(n)
// Output:
// {"key1":"var1","key2":"var2"}
// {"key1":"var1","key2":"var2"}
}
func ExampleNewTreeMap() {
m := gmap.NewTreeMap(gutil.ComparatorString)
m.Set("key2", "var2")
m.Set("key1", "var1")
fmt.Println(m.Map())
// Output:
// map[key1:var1 key2:var2]
}
func ExampleNewTreeMapFrom() {
m := gmap.NewTreeMap(gutil.ComparatorString)
m.Set("key2", "var2")
m.Set("key1", "var1")
fmt.Println(m.Map())
n := gmap.NewListMapFrom(m.Map(), true)
fmt.Println(n.Map())
// Output:
// map[key1:var1 key2:var2]
// map[key1:var1 key2:var2]
}

View File

@ -442,6 +442,51 @@ func ExampleValidator_RequiredWithoutAll() {
// The HusbandName field is required
}
func ExampleValidator_Bail() {
type BizReq struct {
Account string `v:"bail|required|length:6,16|same:QQ"`
QQ string
Password string `v:"required|same:Password2"`
Password2 string `v:"required"`
}
var (
ctx = context.Background()
req = BizReq{
Account: "gf",
QQ: "123456",
Password: "goframe.org",
Password2: "goframe.org",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// output:
// The Account value `gf` length must be between 6 and 16
}
func ExampleValidator_CaseInsensitive() {
type BizReq struct {
Account string `v:"required"`
Password string `v:"required|ci|same:Password2"`
Password2 string `v:"required"`
}
var (
ctx = context.Background()
req = BizReq{
Account: "gf",
Password: "Goframe.org", // Diff from Password2, but because of "ci", rule check passed
Password2: "goframe.org",
}
)
if err := g.Validator().Data(req).Run(ctx); err != nil {
fmt.Println(err)
}
// output:
}
func ExampleValidator_Date() {
type BizReq struct {
Date1 string `v:"date"`