add 08.2.md 08.3.md 08.4.md 08.5.md 08.6.md

This commit is contained in:
chidouhu
2013-11-26 11:35:07 +08:00
parent c3aa481999
commit aaee8481fb
10 changed files with 390 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
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: NOT GOOD!
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)
}