Skip to content

Commit a7c7ec5

Browse files
aimuzgopherbot
authored andcommitted
maps: add examples for All, Keys, Values, Insert, and Collect functions
Change-Id: I4ee61bea9997b822aa1ec2cc3d01b4db5f101e4c GitHub-Last-Rev: d88282a GitHub-Pull-Request: #68696 Reviewed-on: https://go-review.googlesource.com/c/go/+/602315 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 0ea534b commit a7c7ec5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/maps/example_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package maps_test
77
import (
88
"fmt"
99
"maps"
10+
"slices"
1011
"strings"
1112
)
1213

@@ -133,3 +134,60 @@ func ExampleEqualFunc() {
133134
// Output:
134135
// true
135136
}
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+
// Output:
149+
// m2 is: map[one:1 two:2]
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+
// Output:
161+
// [1 10 1000]
162+
}
163+
164+
func ExampleValues() {
165+
m1 := map[int]string{
166+
1: "one",
167+
10: "Ten",
168+
1000: "THOUSAND",
169+
}
170+
keys := slices.Sorted(maps.Values(m1))
171+
fmt.Println(keys)
172+
// Output:
173+
// [THOUSAND Ten one]
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+
// Output:
184+
// m1 is: map[0:zero 1:one 2:two 3:three 1000:THOUSAND]
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+
// Output:
192+
// m1 is: map[0:zero 1:one 2:two 3:three]
193+
}

0 commit comments

Comments
 (0)