1// Copyright 2023 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package maps_test
6
7import (
8	"fmt"
9	"maps"
10	"strings"
11)
12
13func 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	// Output:
32	// 1
33	// 100
34	// 1
35	// 100
36	// 100
37}
38
39func 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	// Output:
71	// m2 is: map[one:1 two:2]
72	// m1 is: map[one:1 two:2]
73	// m2 is: map[one:100 two:2]
74	// m4 is: map[one:[1 2 3] two:[4 5 6]]
75	// m3 is: map[one:[100 2 3] two:[4 5 6]]
76	// m4 is: map[one:[100 2 3] two:[4 5 6]]
77}
78
79func 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 // delete odd values
88	})
89	fmt.Println(m)
90	// Output:
91	// map[four:4 two:2]
92}
93
94func 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	// Output:
114	// true
115	// false
116}
117
118func 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	// Output:
134	// true
135}
136