codekingpro/portable-devtools
114k
1// Copyright 2023 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5package maps_test6 7import (8 "fmt"9 "maps"10 "slices"11 "strings"12)13 14func ExampleClone() {15 m1 := map[string]int{16 "key": 1,17 }18 m2 := maps.Clone(m1)19 m2["key"] = 10020 fmt.Println(m1["key"])21 fmt.Println(m2["key"])22 23 m3 := map[string][]int{24 "key": {1, 2, 3},25 }26 m4 := maps.Clone(m3)27 fmt.Println(m4["key"][0])28 m4["key"][0] = 10029 fmt.Println(m3["key"][0])30 fmt.Println(m4["key"][0])31 32 // Output:33 // 134 // 10035 // 136 // 10037 // 10038}39 40func ExampleCopy() {41 m1 := map[string]int{42 "one": 1,43 "two": 2,44 }45 m2 := map[string]int{46 "one": 10,47 }48 49 maps.Copy(m2, m1)50 fmt.Println("m2 is:", m2)51 52 m2["one"] = 10053 fmt.Println("m1 is:", m1)54 fmt.Println("m2 is:", m2)55 56 m3 := map[string][]int{57 "one": {1, 2, 3},58 "two": {4, 5, 6},59 }60 m4 := map[string][]int{61 "one": {7, 8, 9},62 }63 64 maps.Copy(m4, m3)65 fmt.Println("m4 is:", m4)66 67 m4["one"][0] = 10068 fmt.Println("m3 is:", m3)69 fmt.Println("m4 is:", m4)70 71 // Output:72 // m2 is: map[one:1 two:2]73 // m1 is: map[one:1 two:2]74 // m2 is: map[one:100 two:2]75 // m4 is: map[one:[1 2 3] two:[4 5 6]]76 // m3 is: map[one:[100 2 3] two:[4 5 6]]77 // m4 is: map[one:[100 2 3] two:[4 5 6]]78}79 80func ExampleDeleteFunc() {81 m := map[string]int{82 "one": 1,83 "two": 2,84 "three": 3,85 "four": 4,86 }87 maps.DeleteFunc(m, func(k string, v int) bool {88 return v%2 != 0 // delete odd values89 })90 fmt.Println(m)91 // Output:92 // map[four:4 two:2]93}94 95func ExampleEqual() {96 m1 := map[int]string{97 1: "one",98 10: "Ten",99 1000: "THOUSAND",100 }101 m2 := map[int]string{102 1: "one",103 10: "Ten",104 1000: "THOUSAND",105 }106 m3 := map[int]string{107 1: "one",108 10: "ten",109 1000: "thousand",110 }111 112 fmt.Println(maps.Equal(m1, m2))113 fmt.Println(maps.Equal(m1, m3))114 // Output:115 // true116 // false117}118 119func ExampleEqualFunc() {120 m1 := map[int]string{121 1: "one",122 10: "Ten",123 1000: "THOUSAND",124 }125 m2 := map[int][]byte{126 1: []byte("One"),127 10: []byte("Ten"),128 1000: []byte("Thousand"),129 }130 eq := maps.EqualFunc(m1, m2, func(v1 string, v2 []byte) bool {131 return strings.EqualFold(v1, string(v2))132 })133 fmt.Println(eq)134 // Output:135 // true136}137 138func ExampleAll() {139 m1 := map[string]int{140 "one": 1,141 "two": 2,142 }143 m2 := map[string]int{144 "one": 10,145 }146 maps.Insert(m2, maps.All(m1))147 fmt.Println("m2 is:", m2)148 // Output:149 // m2 is: map[one:1 two:2]150}151 152func ExampleKeys() {153 m1 := map[int]string{154 1: "one",155 10: "Ten",156 1000: "THOUSAND",157 }158 keys := slices.Sorted(maps.Keys(m1))159 fmt.Println(keys)160 // Output:161 // [1 10 1000]162}163 164func ExampleValues() {165 m1 := map[int]string{166 1: "one",167 10: "Ten",168 1000: "THOUSAND",169 }170 values := slices.Sorted(maps.Values(m1))171 fmt.Println(values)172 // Output:173 // [THOUSAND Ten one]174}175 176func ExampleInsert() {177 m1 := map[int]string{178 1000: "THOUSAND",179 }180 s1 := []string{"zero", "one", "two", "three"}181 maps.Insert(m1, slices.All(s1))182 fmt.Println("m1 is:", m1)183 // Output:184 // m1 is: map[0:zero 1:one 2:two 3:three 1000:THOUSAND]185}186 187func ExampleCollect() {188 s1 := []string{"zero", "one", "two", "three"}189 m1 := maps.Collect(slices.All(s1))190 fmt.Println("m1 is:", m1)191 // Output:192 // m1 is: map[0:zero 1:one 2:two 3:three]193}194 