Source file
src/maps/example_test.go
Documentation: maps
1
2
3
4
5 package maps_test
6
7 import (
8 "fmt"
9 "maps"
10 "strings"
11 )
12
13 func ExampleClone() {
14 m1 := map[string]int{
15 "key": 1,
16 }
17 m2 := maps.Clone(m1)
18 m2["key"] = 100
19 fmt.Println(m1["key"])
20 fmt.Println(m2["key"])
21
22 m3 := map[string][]int{
23 "key": {1, 2, 3},
24 }
25 m4 := maps.Clone(m3)
26 fmt.Println(m4["key"][0])
27 m4["key"][0] = 100
28 fmt.Println(m3["key"][0])
29 fmt.Println(m4["key"][0])
30
31
32
33
34
35
36
37 }
38
39 func ExampleCopy() {
40 m1 := map[string]int{
41 "one": 1,
42 "two": 2,
43 }
44 m2 := map[string]int{
45 "one": 10,
46 }
47
48 maps.Copy(m2, m1)
49 fmt.Println("m2 is:", m2)
50
51 m2["one"] = 100
52 fmt.Println("m1 is:", m1)
53 fmt.Println("m2 is:", m2)
54
55 m3 := map[string][]int{
56 "one": {1, 2, 3},
57 "two": {4, 5, 6},
58 }
59 m4 := map[string][]int{
60 "one": {7, 8, 9},
61 }
62
63 maps.Copy(m4, m3)
64 fmt.Println("m4 is:", m4)
65
66 m4["one"][0] = 100
67 fmt.Println("m3 is:", m3)
68 fmt.Println("m4 is:", m4)
69
70
71
72
73
74
75
76
77 }
78
79 func ExampleDeleteFunc() {
80 m := map[string]int{
81 "one": 1,
82 "two": 2,
83 "three": 3,
84 "four": 4,
85 }
86 maps.DeleteFunc(m, func(k string, v int) bool {
87 return v%2 != 0
88 })
89 fmt.Println(m)
90
91
92 }
93
94 func ExampleEqual() {
95 m1 := map[int]string{
96 1: "one",
97 10: "Ten",
98 1000: "THOUSAND",
99 }
100 m2 := map[int]string{
101 1: "one",
102 10: "Ten",
103 1000: "THOUSAND",
104 }
105 m3 := map[int]string{
106 1: "one",
107 10: "ten",
108 1000: "thousand",
109 }
110
111 fmt.Println(maps.Equal(m1, m2))
112 fmt.Println(maps.Equal(m1, m3))
113
114
115
116 }
117
118 func ExampleEqualFunc() {
119 m1 := map[int]string{
120 1: "one",
121 10: "Ten",
122 1000: "THOUSAND",
123 }
124 m2 := map[int][]byte{
125 1: []byte("One"),
126 10: []byte("Ten"),
127 1000: []byte("Thousand"),
128 }
129 eq := maps.EqualFunc(m1, m2, func(v1 string, v2 []byte) bool {
130 return strings.ToLower(v1) == strings.ToLower(string(v2))
131 })
132 fmt.Println(eq)
133
134
135 }
136
View as plain text