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)
- 上一节:[测试键值对是否存在及删除元素](08.2.md)
- 下一节:[map ](08.4.md)
- 下一节:[map 类型的切](08.4.md)

View File

@@ -1,15 +1,14 @@
# 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
import "fmt"
func main() {
func main() {
// Version A:
items := make([]map[int]int, 5)
for i:= range items {
@@ -25,16 +24,18 @@
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 B: Value of items: [map[] map[] map[] map[] map[]]
需要注意的是,应当像A版本那样通过索引使用slice的map项。在B版本中获得的项只是map值的一个拷贝而已所以真正的map元素没有得到初始化。
需要注意的是,应当像 A 版本那样通过索引使用 slice 的 map 元素。在 B 版本中获得的项只是 map 值的一个拷贝而已,所以真正的 map 元素没有得到初始化。
## 链接
##链接
- [目录](directory.md)
- 上一节:[for循环构造方](08.3.md)
- 下一节:[map排序](08.5.md)
- 上一节:[for-range 的配套用](08.3.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拷贝到一个slice再对slice排序使用sort包参见7.6.6然后可以使用slice的for-range方法打印出所有的key和value。

View File

@@ -69,7 +69,8 @@
- 8.1 [声明、初始化和 make](08.1.md)
- 8.2 [测试键值对是否存在及删除元素](08.2.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
- 第10章结构struct与方法method
- 第11章接口interface与反射reflection