Go語(yǔ)言入門(3) - 地圖 Get started with the Go language
今日單詞 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語(yǔ)句將鍵映射到值,與slice一樣,你創(chuàng)建一個(gè)map用make而不是用new,在以下示例中,我們將字符串鍵映射到整數(shù)值。此代碼演示了地圖元素的插入、更新、刪除和測(cè)試。
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:
程序的輸出結(jié)果如下:
The value: 42
The value: 48
The value: 0
The value: 0 Present? false評(píng)論
圖片
表情
