github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_example_test.go (about) 1 package gmap_test 2 3 import ( 4 "fmt" 5 "github.com/zhongdalu/gf/g/container/gmap" 6 ) 7 8 func Example_Normal_Basic() { 9 m := gmap.New() 10 11 //Add data 12 m.Set("key1", "val1") 13 14 //Print size 15 fmt.Println(m.Size()) 16 //output 1 17 18 add_map := make(map[interface{}]interface{}) 19 add_map["key2"] = "val2" 20 add_map["key3"] = "val3" 21 add_map[1] = 1 22 23 fmt.Println(m.Values()) 24 25 //Batch add data 26 m.Sets(add_map) 27 28 //Gets the value of the corresponding key 29 key3_val := m.Get("key3") 30 fmt.Println(key3_val) 31 32 //Get the value by key, or set it with given key-value if not exist. 33 get_or_set_val := m.GetOrSet("key4", "val4") 34 fmt.Println(get_or_set_val) 35 36 // Set key-value if the key does not exist, then return true; or else return false. 37 is_set := m.SetIfNotExist("key3", "val3") 38 fmt.Println(is_set) 39 40 //Remove key 41 m.Remove("key2") 42 fmt.Println(m.Keys()) 43 44 //Batch remove keys 45 remove_keys := []interface{}{"key1", 1} 46 m.Removes(remove_keys) 47 fmt.Println(m.Keys()) 48 49 //Contains checks whether a key exists. 50 is_contain := m.Contains("key3") 51 fmt.Println(is_contain) 52 53 //Flip exchanges key-value of the map, it will change key-value to value-key. 54 m.Flip() 55 fmt.Println(m.Map()) 56 57 // Clear deletes all data of the map, 58 m.Clear() 59 60 fmt.Println(m.Size()) 61 62 } 63 func Example_Normal_Merge() { 64 m1 := gmap.New() 65 m2 := gmap.New() 66 m1.Set("key1", "val1") 67 m2.Set("key2", "val2") 68 m1.Merge(m2) 69 fmt.Println(m1.Map()) 70 }