2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-06-04 20:45:18 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-04-08 17:02:57 +08:00
|
|
|
package gmap_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-04 20:45:18 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/container/gmap"
|
2019-04-08 17:02:57 +08:00
|
|
|
)
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
func ExampleNew() {
|
2019-04-08 17:02:57 +08:00
|
|
|
m := gmap.New()
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Add data.
|
2019-04-08 17:02:57 +08:00
|
|
|
m.Set("key1", "val1")
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Print size.
|
2019-04-08 17:02:57 +08:00
|
|
|
fmt.Println(m.Size())
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
addMap := make(map[interface{}]interface{})
|
|
|
|
addMap["key2"] = "val2"
|
|
|
|
addMap["key3"] = "val3"
|
|
|
|
addMap[1] = 1
|
2019-04-08 17:02:57 +08:00
|
|
|
|
|
|
|
fmt.Println(m.Values())
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Batch add data.
|
|
|
|
m.Sets(addMap)
|
2019-04-08 17:02:57 +08:00
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Gets the value of the corresponding key.
|
|
|
|
fmt.Println(m.Get("key3"))
|
2019-04-08 17:02:57 +08:00
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Get the value by key, or set it with given key-value if not exist.
|
|
|
|
fmt.Println(m.GetOrSet("key4", "val4"))
|
2019-04-08 17:02:57 +08:00
|
|
|
|
|
|
|
// Set key-value if the key does not exist, then return true; or else return false.
|
2020-06-10 19:47:25 +08:00
|
|
|
fmt.Println(m.SetIfNotExist("key3", "val3"))
|
2019-04-08 17:02:57 +08:00
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Remove key
|
2019-04-08 17:02:57 +08:00
|
|
|
m.Remove("key2")
|
|
|
|
fmt.Println(m.Keys())
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Batch remove keys.
|
|
|
|
m.Removes([]interface{}{"key1", 1})
|
2019-04-08 17:02:57 +08:00
|
|
|
fmt.Println(m.Keys())
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Contains checks whether a key exists.
|
|
|
|
fmt.Println(m.Contains("key3"))
|
2019-04-08 17:02:57 +08:00
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Flip exchanges key-value of the map, it will change key-value to value-key.
|
2019-04-08 17:02:57 +08:00
|
|
|
m.Flip()
|
|
|
|
fmt.Println(m.Map())
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
// Clear deletes all data of the map.
|
2019-04-08 17:02:57 +08:00
|
|
|
m.Clear()
|
|
|
|
|
|
|
|
fmt.Println(m.Size())
|
2020-06-10 19:47:25 +08:00
|
|
|
|
|
|
|
// May Output:
|
|
|
|
// 1
|
|
|
|
// [val1]
|
|
|
|
// val3
|
|
|
|
// val4
|
|
|
|
// false
|
|
|
|
// [key4 key1 key3 1]
|
|
|
|
// [key4 key3]
|
|
|
|
// true
|
|
|
|
// map[val3:key3 val4:key4]
|
|
|
|
// 0
|
2020-06-04 20:45:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
func ExampleAnyAnyMap_Keys() {
|
2020-06-04 20:45:18 +08:00
|
|
|
var m gmap.Map
|
|
|
|
m.Sets(g.MapAnyAny{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
|
|
|
"k3": "v3",
|
|
|
|
"k4": "v4",
|
|
|
|
})
|
|
|
|
fmt.Println(m.Keys())
|
|
|
|
fmt.Println(m.Values())
|
|
|
|
|
|
|
|
// May Output:
|
|
|
|
// [k1 k2 k3 k4]
|
|
|
|
// [v2 v3 v4 v1]
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
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())
|
|
|
|
|
|
|
|
// May Output:
|
|
|
|
// [k1 k2 k3 k4]
|
|
|
|
// [v2 v3 v4 v1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleAnyAnyMap_Flip() {
|
2020-06-04 20:45:18 +08:00
|
|
|
var m gmap.Map
|
|
|
|
m.Sets(g.MapAnyAny{
|
|
|
|
"k1": "v1",
|
|
|
|
"k2": "v2",
|
|
|
|
})
|
|
|
|
m.Flip()
|
|
|
|
fmt.Println(m.Map())
|
|
|
|
|
|
|
|
// May Output:
|
|
|
|
// map[v1:k1 v2:k2]
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
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() {
|
2020-06-04 20:45:18 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
func ExampleAnyAnyMap_FilterEmpty() {
|
|
|
|
m := gmap.NewFrom(g.MapAnyAny{
|
2020-06-04 20:45:18 +08:00
|
|
|
"k1": "",
|
|
|
|
"k2": nil,
|
|
|
|
"k3": 0,
|
|
|
|
"k4": 1,
|
|
|
|
})
|
2020-06-10 19:47:25 +08:00
|
|
|
m.FilterEmpty()
|
|
|
|
fmt.Println(m.Map())
|
|
|
|
|
|
|
|
// May Output:
|
|
|
|
// map[k4:1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleAnyAnyMap_FilterNil() {
|
|
|
|
m := gmap.NewFrom(g.MapAnyAny{
|
2020-06-04 20:45:18 +08:00
|
|
|
"k1": "",
|
|
|
|
"k2": nil,
|
|
|
|
"k3": 0,
|
|
|
|
"k4": 1,
|
|
|
|
})
|
2020-06-10 19:47:25 +08:00
|
|
|
m.FilterNil()
|
|
|
|
fmt.Println(m.Map())
|
2019-04-08 17:02:57 +08:00
|
|
|
|
2020-06-09 16:24:54 +08:00
|
|
|
// May Output:
|
2020-06-04 20:45:18 +08:00
|
|
|
// map[k1: k3:0 k4:1]
|
2019-04-08 17:02:57 +08:00
|
|
|
}
|
2020-06-04 20:45:18 +08:00
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
func ExampleAnyAnyMap_SetIfNotExist() {
|
2020-06-04 20:45:18 +08:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:25 +08:00
|
|
|
func ExampleAnyAnyMap_Merge() {
|
2020-06-04 20:45:18 +08:00
|
|
|
var m1, m2 gmap.Map
|
2019-06-19 09:06:52 +08:00
|
|
|
m1.Set("key1", "val1")
|
|
|
|
m2.Set("key2", "val2")
|
2020-06-04 20:45:18 +08:00
|
|
|
m1.Merge(&m2)
|
2019-04-08 17:02:57 +08:00
|
|
|
fmt.Println(m1.Map())
|
2020-06-04 20:45:18 +08:00
|
|
|
|
|
|
|
// May Output:
|
|
|
|
// map[key1:val1 key2:val2]
|
2019-04-08 17:02:57 +08:00
|
|
|
}
|