Source file
src/maps/example_test.go
1
2
3
4
5 package maps_test
6
7 import (
8 "fmt"
9 "maps"
10 "slices"
11 "strings"
12 )
13
14 func ExampleClone() {
15 m1 := map[string]int{
16 "key": 1,
17 }
18 m2 := maps.Clone(m1)
19 m2["key"] = 100
20 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] = 100
29 fmt.Println(m3["key"][0])
30 fmt.Println(m4["key"][0])
31
32
33
34
35
36
37
38 }
39
40 func 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"] = 100
53 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] = 100
68 fmt.Println("m3 is:", m3)
69 fmt.Println("m4 is:", m4)
70
71
72
73
74
75
76
77
78 }
79
80 func 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
89 })
90 fmt.Println(m)
91
92
93 }
94
95 func 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
115
116
117 }
118
119 func 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.ToLower(v1) == strings.ToLower(string(v2))
132 })
133 fmt.Println(eq)
134
135
136 }
137
138 func 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
149
150 }
151
152 func 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
161
162 }
163
164 func 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
173
174 }
175
176 func 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
184
185 }
186
187 func ExampleCollect() {
188 s1 := []string{"zero", "one", "two", "three"}
189 m1 := maps.Collect(slices.All(s1))
190 fmt.Println("m1 is:", m1)
191
192
193 }
194
View as plain text