1
0
mirror of https://github.com/golang/go synced 2024-11-23 14:50:07 -07:00

maps: add examples for Clone,Copy and Equal

Change-Id: I72adaf48588e7d6cffbc0ee8005decda06808e84
Reviewed-on: https://go-review.googlesource.com/c/go/+/534055
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: shuang cui <imcusg@gmail.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
cui fliter 2023-10-10 10:51:19 +08:00 committed by Gopher Robot
parent 53d000d75b
commit 2470d05872

View File

@ -10,6 +10,72 @@ import (
"strings"
)
func ExampleClone() {
m1 := map[string]int{
"key": 1,
}
m2 := maps.Clone(m1)
m2["key"] = 100
fmt.Println(m1["key"])
fmt.Println(m2["key"])
m3 := map[string][]int{
"key": {1, 2, 3},
}
m4 := maps.Clone(m3)
fmt.Println(m4["key"][0])
m4["key"][0] = 100
fmt.Println(m3["key"][0])
fmt.Println(m4["key"][0])
// Output:
// 1
// 100
// 1
// 100
// 100
}
func ExampleCopy() {
m1 := map[string]int{
"one": 1,
"two": 2,
}
m2 := map[string]int{
"one": 10,
}
maps.Copy(m2, m1)
fmt.Println("m2 is:", m2)
m2["one"] = 100
fmt.Println("m1 is:", m1)
fmt.Println("m2 is:", m2)
m3 := map[string][]int{
"one": {1, 2, 3},
"two": {4, 5, 6},
}
m4 := map[string][]int{
"one": {7, 8, 9},
}
maps.Copy(m4, m3)
fmt.Println("m4 is:", m4)
m4["one"][0] = 100
fmt.Println("m3 is:", m3)
fmt.Println("m4 is:", m4)
// Output:
// m2 is: map[one:1 two:2]
// m1 is: map[one:1 two:2]
// m2 is: map[one:100 two:2]
// m4 is: map[one:[1 2 3] two:[4 5 6]]
// m3 is: map[one:[100 2 3] two:[4 5 6]]
// m4 is: map[one:[100 2 3] two:[4 5 6]]
}
func ExampleDeleteFunc() {
m := map[string]int{
"one": 1,
@ -25,6 +91,30 @@ func ExampleDeleteFunc() {
// map[four:4 two:2]
}
func ExampleEqual() {
m1 := map[int]string{
1: "one",
10: "Ten",
1000: "THOUSAND",
}
m2 := map[int]string{
1: "one",
10: "Ten",
1000: "THOUSAND",
}
m3 := map[int]string{
1: "one",
10: "ten",
1000: "thousand",
}
fmt.Println(maps.Equal(m1, m2))
fmt.Println(maps.Equal(m1, m3))
// Output:
// true
// false
}
func ExampleEqualFunc() {
m1 := map[int]string{
1: "one",