github.com/gogf/gf@v1.16.9/container/gmap/gmap_z_example_any_any_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gmap_test 8 9 import ( 10 "fmt" 11 "github.com/gogf/gf/frame/g" 12 13 "github.com/gogf/gf/container/gmap" 14 ) 15 16 func ExampleNew() { 17 m := gmap.New() 18 19 // Add data. 20 m.Set("key1", "val1") 21 22 // Print size. 23 fmt.Println(m.Size()) 24 25 addMap := make(map[interface{}]interface{}) 26 addMap["key2"] = "val2" 27 addMap["key3"] = "val3" 28 addMap[1] = 1 29 30 fmt.Println(m.Values()) 31 32 // Batch add data. 33 m.Sets(addMap) 34 35 // Gets the value of the corresponding key. 36 fmt.Println(m.Get("key3")) 37 38 // Get the value by key, or set it with given key-value if not exist. 39 fmt.Println(m.GetOrSet("key4", "val4")) 40 41 // Set key-value if the key does not exist, then return true; or else return false. 42 fmt.Println(m.SetIfNotExist("key3", "val3")) 43 44 // Remove key 45 m.Remove("key2") 46 fmt.Println(m.Keys()) 47 48 // Batch remove keys. 49 m.Removes([]interface{}{"key1", 1}) 50 fmt.Println(m.Keys()) 51 52 // Contains checks whether a key exists. 53 fmt.Println(m.Contains("key3")) 54 55 // Flip exchanges key-value of the map, it will change key-value to value-key. 56 m.Flip() 57 fmt.Println(m.Map()) 58 59 // Clear deletes all data of the map. 60 m.Clear() 61 62 fmt.Println(m.Size()) 63 64 // May Output: 65 // 1 66 // [val1] 67 // val3 68 // val4 69 // false 70 // [key4 key1 key3 1] 71 // [key4 key3] 72 // true 73 // map[val3:key3 val4:key4] 74 // 0 75 } 76 77 func ExampleAnyAnyMap_Keys() { 78 var m gmap.Map 79 m.Sets(g.MapAnyAny{ 80 "k1": "v1", 81 "k2": "v2", 82 "k3": "v3", 83 "k4": "v4", 84 }) 85 fmt.Println(m.Keys()) 86 fmt.Println(m.Values()) 87 88 // May Output: 89 // [k1 k2 k3 k4] 90 // [v2 v3 v4 v1] 91 } 92 93 func ExampleAnyAnyMap_Values() { 94 var m gmap.Map 95 m.Sets(g.MapAnyAny{ 96 "k1": "v1", 97 "k2": "v2", 98 "k3": "v3", 99 "k4": "v4", 100 }) 101 fmt.Println(m.Keys()) 102 fmt.Println(m.Values()) 103 104 // May Output: 105 // [k1 k2 k3 k4] 106 // [v2 v3 v4 v1] 107 } 108 109 func ExampleAnyAnyMap_Flip() { 110 var m gmap.Map 111 m.Sets(g.MapAnyAny{ 112 "k1": "v1", 113 "k2": "v2", 114 }) 115 m.Flip() 116 fmt.Println(m.Map()) 117 118 // May Output: 119 // map[v1:k1 v2:k2] 120 } 121 122 func ExampleAnyAnyMap_Pop() { 123 var m gmap.Map 124 m.Sets(g.MapAnyAny{ 125 "k1": "v1", 126 "k2": "v2", 127 "k3": "v3", 128 "k4": "v4", 129 }) 130 fmt.Println(m.Pop()) 131 fmt.Println(m.Pops(2)) 132 fmt.Println(m.Size()) 133 134 // May Output: 135 // k1 v1 136 // map[k2:v2 k4:v4] 137 // 1 138 } 139 140 func ExampleAnyAnyMap_Pops() { 141 var m gmap.Map 142 m.Sets(g.MapAnyAny{ 143 "k1": "v1", 144 "k2": "v2", 145 "k3": "v3", 146 "k4": "v4", 147 }) 148 fmt.Println(m.Pop()) 149 fmt.Println(m.Pops(2)) 150 fmt.Println(m.Size()) 151 152 // May Output: 153 // k1 v1 154 // map[k2:v2 k4:v4] 155 // 1 156 } 157 158 func ExampleAnyAnyMap_FilterEmpty() { 159 m := gmap.NewFrom(g.MapAnyAny{ 160 "k1": "", 161 "k2": nil, 162 "k3": 0, 163 "k4": 1, 164 }) 165 m.FilterEmpty() 166 fmt.Println(m.Map()) 167 168 // May Output: 169 // map[k4:1] 170 } 171 172 func ExampleAnyAnyMap_FilterNil() { 173 m := gmap.NewFrom(g.MapAnyAny{ 174 "k1": "", 175 "k2": nil, 176 "k3": 0, 177 "k4": 1, 178 }) 179 m.FilterNil() 180 fmt.Println(m.Map()) 181 182 // May Output: 183 // map[k1: k3:0 k4:1] 184 } 185 186 func ExampleAnyAnyMap_SetIfNotExist() { 187 var m gmap.Map 188 fmt.Println(m.SetIfNotExist("k1", "v1")) 189 fmt.Println(m.SetIfNotExist("k1", "v1")) 190 fmt.Println(m.Map()) 191 192 // Output: 193 // true 194 // false 195 // map[k1:v1] 196 } 197 198 func ExampleAnyAnyMap_Merge() { 199 var m1, m2 gmap.Map 200 m1.Set("key1", "val1") 201 m2.Set("key2", "val2") 202 m1.Merge(&m2) 203 fmt.Println(m1.Map()) 204 205 // May Output: 206 // map[key1:val1 key2:val2] 207 }