mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 00:11:36 +08:00
8.3
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
#8.3 map分片
|
||||
# 8.3 map 分片
|
||||
|
||||
191
|
||||
|
||||
假设我们想获取一个map的分片,我们必须使用两次make()方法,第一次分配slice,第二次分配slice的每个map元素(参见下面的例子8.3)。
|
||||
|
||||
示例 8.3 [maps_forrange.go](examples/chapter_8/maps_forrange.go)
|
||||
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
|
||||
func main() {
|
||||
// Version A:
|
||||
items := make([]map[int]int, 5)
|
||||
@@ -14,7 +17,7 @@
|
||||
items[i][1] = 2
|
||||
}
|
||||
fmt.Printf("Version A: Value of items: %v\n", items)
|
||||
|
||||
|
||||
// Version B: NOT GOOD!
|
||||
items2 := make([]map[int]int, 5)
|
||||
for _, item := range items2 {
|
||||
|
Reference in New Issue
Block a user