Go语言入门(3) - 地图 Get started with the Go language
Java研发军团
共 981字,需浏览 2分钟
·
2022-11-22 11:22
今日单词 Words for today
values 值
demonstrates 演示
print 打印
output 输出
Maps
The Go map
statement maps keys to values. As with slice
, you create a map with make
, not new
. In the following example, we map string keys to integer values. This code demonstrates inserting, updating, deleting, and testing for map elements.
GO的map语句将键映射到值,与slice一样,你创建一个map用make而不是用new,在以下示例中,我们将字符串键映射到整数值。此代码演示了地图元素的插入、更新、删除和测试。
package main
import "fmt"
func main() {
m := make(map[string]int)
m["Answer"] = 42
fmt.Println("The value:", m["Answer"])
m["Answer"] = 48
fmt.Println("The value:", m["Answer"])
delete(m, "Answer")
fmt.Println("The value:", m["Answer"])
v, ok := m["Answer"]
fmt.Println("The value:", v, "Present?", ok)
}
Here is the program's print output:
程序的输出结果如下:
The value: 42
The value: 48
The value: 0
The value: 0 Present? false
评论