mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
28 lines
738 B
Go
28 lines
738 B
Go
// slice_maps.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func main() {
|
|
// Version A:
|
|
items := make([]map[int]int, 5)
|
|
for i := range items {
|
|
items[i] = make(map[int]int, 1)
|
|
items[i][1] = 2
|
|
}
|
|
fmt.Printf("Version A: Value of items: %v\n", items)
|
|
// Version B:
|
|
items2 := make([]map[int]int, 5)
|
|
for _, item := range items2 {
|
|
item = make(map[int]int, 1) // item is only a copy of the slice element.
|
|
item[1] = 2 // This 'item' will be lost on the next iteration.
|
|
}
|
|
fmt.Printf("Version B: Value of items: %v\n", items2)
|
|
}
|
|
/* Output:
|
|
Version A: Value of items: [map[1:2] map[1:2] map[1:2] map[1:2] map[1:2]]
|
|
Version B: Value of items: [map[] map[] map[] map[] map[]]
|
|
*/
|