mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-19 03:50:11 +08:00
modified: 18.1.md
new file: 18.10.md new file: 18.11.md new file: 18.2.md new file: 18.3.md new file: 18.4.md modified: 18.5.md new file: 18.6.md new file: 18.7.md new file: 18.8.md new file: 18.9.md modified: directory.md
This commit is contained in:
28
eBook/18.3.md
Normal file
28
eBook/18.3.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# 18.3 映射
|
||||
|
||||
创建: `map1 := make(map[keytype]valuetype)`
|
||||
初始化: `map1 := map[string]int{"one": 1, "two": 2}`
|
||||
|
||||
(1)如何使用`for`或者`for-range`遍历一个映射:
|
||||
|
||||
```go
|
||||
for key, value := range map1 {
|
||||
…
|
||||
}
|
||||
```
|
||||
|
||||
(2)如何在一个映射中检测键key1是否存在:
|
||||
|
||||
`val1, isPresent = map1[key1]`
|
||||
|
||||
返回值: 键`key1`对应的值或者`0`, `true`或者`false`
|
||||
|
||||
(3)如何在映射中删除一个键:
|
||||
|
||||
`delete(map1, key1)`
|
||||
|
||||
## 链接
|
||||
|
||||
- [目录](directory.md)
|
||||
- 上一章:[运算符模板和接口](17.4.md)
|
||||
- 下一节:[字符串](18.1.md)
|
Reference in New Issue
Block a user