This commit is contained in:
Unknwon
2015-02-23 02:49:28 -05:00
parent d8608c6c23
commit 80344c8375
5 changed files with 41 additions and 36 deletions

View File

@@ -9,7 +9,7 @@
## 翻译进度 ## 翻译进度
8.3 [for-range 的配套用法](eBook/08.3.md) 8.4 [map 类型的切片](eBook/08.4.md)
## 支持本书 ## 支持本书

View File

@@ -68,4 +68,4 @@ for key := range capitals {
- [目录](directory.md) - [目录](directory.md)
- 上一节:[测试键值对是否存在及删除元素](08.2.md) - 上一节:[测试键值对是否存在及删除元素](08.2.md)
- 下一节:[map ](08.4.md) - 下一节:[map 类型的切](08.4.md)

View File

@@ -1,40 +1,41 @@
# 8.3 map # 8.3 map 类型的切
191 假设我们想获取一个 map 类型的切片,我们必须使用两次 make() 方法,第一次分配 slice第二次分配 slice 中每个 map 元素(参见下面的例子 8.3)。
假设我们想获取一个map的分片我们必须使用两次make()方法第一次分配slice第二次分配slice的每个map元素参见下面的例子8.3)。 示例 8.3 [maps_forrange.go](examples/chapter_8/maps_forrange.go)
示例 8.3 [maps_forrange.go](examples/chapter_8/maps_forrange.go) ```go
package main
import "fmt"
package main func main() {
import "fmt" // 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)
func main() { // Version B: NOT GOOD!
// Version A: items2 := make([]map[int]int, 5)
items := make([]map[int]int, 5) for _, item := range items2 {
for i:= range items { item = make(map[int]int, 1) // item is only a copy of the slice element.
items[i] = make(map[int]int, 1) item[1] = 2 // This 'item' will be lost on the next iteration.
items[i][1] = 2 }
} fmt.Printf("Version B: Value of items: %v\n", items2)
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)
}
输出结果: 输出结果:
Version A: Value of items: [map[1:2] map[1:2] map[1:2] map[1:2] map[1:2]] 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[]] Version B: Value of items: [map[] map[] map[] map[] map[]]
需要注意的是,应当像A版本那样通过索引使用slice的map项。在B版本中获得的项只是map值的一个拷贝而已所以真正的map元素没有得到初始化。 需要注意的是,应当像 A 版本那样通过索引使用 slice 的 map 元素。在 B 版本中获得的项只是 map 值的一个拷贝而已,所以真正的 map 元素没有得到初始化。
## 链接
##链接
- [目录](directory.md) - [目录](directory.md)
- 上一节:[for循环构造方](08.3.md) - 上一节:[for-range 的配套用](08.3.md)
- 下一节:[map排序](08.5.md) - 下一节:[map排序](08.5.md)

View File

@@ -1,4 +1,7 @@
#8.5 map排序 # 8.5 map排序
192
map默认是无序的不管是按照key还是按照value默认都不排序参见8.3节) map默认是无序的不管是按照key还是按照value默认都不排序参见8.3节)
如果你想为map排序需要将key或者value拷贝到一个slice再对slice排序使用sort包参见7.6.6然后可以使用slice的for-range方法打印出所有的key和value。 如果你想为map排序需要将key或者value拷贝到一个slice再对slice排序使用sort包参见7.6.6然后可以使用slice的for-range方法打印出所有的key和value。
@@ -6,21 +9,21 @@ map默认是无序的不管是按照key还是按照value默认都不排序
下面有一个示例: 下面有一个示例:
示例 8.6 [sort_map.go](examples/chapter_8/sort_map.go) 示例 8.6 [sort_map.go](examples/chapter_8/sort_map.go)
// the telephone alphabet: // the telephone alphabet:
package main package main
import ( import (
"fmt" "fmt"
"sort" "sort"
) )
var ( var (
barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23, barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
"delta": 87, "echo": 56, "foxtrot": 12, "delta": 87, "echo": 56, "foxtrot": 12,
"golf": 34, "hotel": 16, "indio": 87, "golf": 34, "hotel": 16, "indio": 87,
"juliet": 65, "kili": 43, "lima": 98} "juliet": 65, "kili": 43, "lima": 98}
) )
func main() { func main() {
fmt.Println("unsorted:") fmt.Println("unsorted:")
for k, v := range barVal { for k, v := range barVal {
@@ -42,9 +45,9 @@ map默认是无序的不管是按照key还是按照value默认都不排序
输出结果: 输出结果:
unsorted: unsorted:
Key: bravo, Value: 56 / Key: echo, Value: 56 / Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: alpha, Value: 34 / Key: charlie, Value: 23 / Key: delta, Value: 87 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 / Key: kili, Value: 43 / Key: lima, Value: 98 / Key: bravo, Value: 56 / Key: echo, Value: 56 / Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: alpha, Value: 34 / Key: charlie, Value: 23 / Key: delta, Value: 87 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 / Key: kili, Value: 43 / Key: lima, Value: 98 /
sorted: sorted:
Key: alpha, Value: 34 / Key: bravo, Value: 56 / Key: charlie, Value: 23 / Key: delta, Value: 87 / Key: echo, Value: 56 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 / Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: kili, Value: 43 / Key: lima, Value: 98 / [fangjun@st01-dstream-0001.st01.baidu.com go]$ sz -be sort_map.go Key: alpha, Value: 34 / Key: bravo, Value: 56 / Key: charlie, Value: 23 / Key: delta, Value: 87 / Key: echo, Value: 56 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 / Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: kili, Value: 43 / Key: lima, Value: 98 / [fangjun@st01-dstream-0001.st01.baidu.com go]$ sz -be sort_map.go
但是如果你想要一个排序的列表你最好使用结构体slice这样会更有效 但是如果你想要一个排序的列表你最好使用结构体slice这样会更有效

View File

@@ -69,7 +69,8 @@
- 8.1 [声明、初始化和 make](08.1.md) - 8.1 [声明、初始化和 make](08.1.md)
- 8.2 [测试键值对是否存在及删除元素](08.2.md) - 8.2 [测试键值对是否存在及删除元素](08.2.md)
- 8.3 [for-range 的配套用法](08.3.md) - 8.3 [for-range 的配套用法](08.3.md)
- 8.4 [map ](08.4.md) - 8.4 [map 类型的切](08.4.md)
- 8.5 [map 的排序](08.5.md)
- 第9章package - 第9章package
- 第10章结构struct与方法method - 第10章结构struct与方法method
- 第11章接口interface与反射reflection - 第11章接口interface与反射reflection