From 2fc3d736883c73899880501adc589b5a1057cd88 Mon Sep 17 00:00:00 2001 From: Tinywan <756684177@qq.com> Date: Fri, 10 Aug 2018 09:19:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E6=84=8F=EF=BC=9Am4=E5=92=8Cm5?= =?UTF-8?q?=E4=B8=A4=E7=A7=8D=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E7=AD=89=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tinywan <756684177@qq.com> --- docs/golang_tutorial_13.md | 62 +++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/docs/golang_tutorial_13.md b/docs/golang_tutorial_13.md index d535eff..137a1f2 100644 --- a/docs/golang_tutorial_13.md +++ b/docs/golang_tutorial_13.md @@ -57,7 +57,7 @@ func main() { personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) } -``` +``` 上面的程序输出:`personSalary map contents: map[steve:12000 jamie:15000 mike:9000]`。 @@ -78,13 +78,13 @@ func main() { personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) } -``` +``` 上面的程序在声明 personSalary 的同时向其中插入了两个元素。接着插入了一个以 "mike" 为键的元素。程序的输出为: ```golang personSalary map contents: map[steve:12000 jamie:15000 mike:9000] -``` +``` `string` 并不是可以作为键的唯一类型,其他所有可以比较的类型,比如,布尔类型,整型,浮点型,复数类型都可以作为键。如果你想了解更多关于可比较类型的话,请参阅:http://golang.org/ref/spec#Comparison_operators @@ -108,7 +108,7 @@ func main() { employee := "jamie" fmt.Println("Salary of", employee, "is", personSalary[employee]) } -``` +``` 上面的程序非常简单。员工 `jamie` 的工资被取出并打印。程序的输出为:`Salary of jamie is 15000`。 @@ -131,14 +131,14 @@ func main() { fmt.Println("Salary of", employee, "is", personSalary[employee]) fmt.Println("Salary of joe is", personSalary["joe"]) } -``` +``` 上面的程序输出为: ```golang Salary of jamie is 15000 Salary of joe is 0 -``` +``` 上面的程序返回 `joe` 的工资为` 0`。我们没有得到任何运行时错误说明键 joe 在 `personSalary` 中不存在。 @@ -146,7 +146,7 @@ Salary of joe is 0 ```golang value, ok := map[key] -``` +``` 上面的语法可以检测一个特定的键是否存在于 map 中。如果 `ok` 是 `true`,则键存在,value 被赋值为对应的值。如果 `ok` 为 `false`,则表示键不存在。 @@ -172,13 +172,13 @@ func main() { } } -``` +``` 在上面的程序中,第 15 行,`ok` 应该为 `false` ,因为 `joe` 不存在。因此程序的输出为: ```golang joe not found -``` +``` range for 可用于遍历 map 中所有的元素(译者注:这里 range 操作符会返回 map 的键和值)。 @@ -200,7 +200,7 @@ func main() { fmt.Printf("personSalary[%s] = %d\n", key, value) } } -``` +``` 上面的程序输出如下: @@ -209,7 +209,7 @@ All items of a map personSalary[mike] = 9000 personSalary[steve] = 12000 personSalary[jamie] = 15000 -``` +``` 值得注意的是,因为 map 是无序的,因此对于程序的每次执行,不能保证使用 range for 遍历 map 的顺序总是一致的。 @@ -235,14 +235,14 @@ func main() { fmt.Println("map after deletion", personSalary) } -``` +``` 上面的程序删除以 `steve` 为键的元素。程序输出为: ```golang map before deletion map[steve:12000 jamie:15000 mike:9000] map after deletion map[mike:9000 jamie:15000] -``` +``` ## map 的大小 @@ -264,7 +264,7 @@ func main() { fmt.Println("length is", len(personSalary)) } -``` +``` 上面程序中,`len(personSalary) `获取 `personSalary` 的大小。上面的程序输出:`length is 3`。 @@ -290,14 +290,14 @@ func main() { newPersonSalary["mike"] = 18000 fmt.Println("Person salary changed", personSalary) } -``` +``` 上面的程序中,第 14 行,`personSalary` 赋值给 `newPersonSalary`。下一行,将 `newPersonSalary` 中 `mike` 的工资改为 `18000`。那么在 `personSalary` 中 `mike` 的工资也将变为 `18000`。程序的输出如下: ```golang Original person salary map[steve:12000 jamie:15000 mike:9000] Person salary changed map[jamie:15000 mike:18000 steve:12000] -``` +``` 将 map 作为参数传递给函数也是一样的。在函数中对 map 的任何修改都会影响在调用函数中看到。 @@ -319,7 +319,7 @@ func main() { if map1 == map2 { } } -``` +``` 上面的程序将会报错:`invalid operation: map1 == map2 (map can only be compared to nil)`。 @@ -327,7 +327,9 @@ func main() { 我(原文作者)已经将我们讨论的所有概念汇总到一个程序中,你可以从 [github](https://github.com/golangbot/arraysandslices) 下载。 -## 知识扩展 +希望你喜欢阅读。请留下宝贵的意见和反馈:) + +## 以下为扩展知识 [Go编程基础视频教程笔记](https://study.163.com/course/courseLearn.htm?courseId=306002#/learn/video?lessonId=421019&courseId=306002) @@ -340,11 +342,12 @@ import ( ) func main(){ - // 方式一 - var m map[int]string // 声明一个map + // 方式一 + var m map[int]string // 声明一个map,此时的 map == nil fmt.Println(m) - m = map[int]string{} // 初始化一个map + m = map[int]string{} // 初始化一个map,此时的 map != nil,是map[] fmt.Println(m) + // 以上两种的区别在于有没有被初始化容量 // 方式二 var m2 map[int]string = map[int]string{} @@ -353,7 +356,18 @@ func main(){ // 方式三 m3 := map[int]string{} fmt.Println(m3) - + + // 方式四 + m4 := map[string]string{ + "name":"Tinywan", + "school":"BAT_UN" + } + fmt.Println(m4) + m5 := make(map[string][string]) + m5["name"] = "Linux" + m5["school"] = "Unix" + // 注意:m4和m5两种初始化的方式等价 + // 设置、获取、删除 m3[1] = "Tinywan" a := m3[1] @@ -414,6 +428,4 @@ func main(){ sort.Ints(slice01) fmt.Println(slice01) // 有序的数组:[1 2 3 4 5 6] } -``` - -希望你喜欢阅读。请留下宝贵的意见和反馈:) \ No newline at end of file +``` \ No newline at end of file