diff --git a/README.md b/README.md index a6fa014..1e75b23 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ## 翻译进度 -8.3 [for-range 的配套用法](eBook/08.3.md) +8.4 [map 类型的切片](eBook/08.4.md) ## 支持本书 diff --git a/eBook/08.3.md b/eBook/08.3.md index e91fc5b..e8c5e1e 100644 --- a/eBook/08.3.md +++ b/eBook/08.3.md @@ -68,4 +68,4 @@ for key := range capitals { - [目录](directory.md) - 上一节:[测试键值对是否存在及删除元素](08.2.md) -- 下一节:[map 分片](08.4.md) +- 下一节:[map 类型的切片](08.4.md) diff --git a/eBook/08.4.md b/eBook/08.4.md index 8243969..68ffb89 100644 --- a/eBook/08.4.md +++ b/eBook/08.4.md @@ -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 - 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) - 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) - } + // 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 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) diff --git a/eBook/08.5.md b/eBook/08.5.md index 2808530..784711f 100644 --- a/eBook/08.5.md +++ b/eBook/08.5.md @@ -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。 @@ -6,21 +9,21 @@ map默认是无序的,不管是按照key还是按照value默认都不排序( 下面有一个示例: 示例 8.6 [sort_map.go](examples/chapter_8/sort_map.go) - + // the telephone alphabet: package main import ( "fmt" "sort" ) - + var ( barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23, "delta": 87, "echo": 56, "foxtrot": 12, "golf": 34, "hotel": 16, "indio": 87, "juliet": 65, "kili": 43, "lima": 98} ) - + func main() { fmt.Println("unsorted:") for k, v := range barVal { @@ -42,9 +45,9 @@ map默认是无序的,不管是按照key还是按照value默认都不排序( 输出结果: 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: - 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,这样会更有效: diff --git a/eBook/directory.md b/eBook/directory.md index 46e11e7..83f58e3 100644 --- a/eBook/directory.md +++ b/eBook/directory.md @@ -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)