Go 自学:map关联数组
以下代码展示了如何建立一个map。
我们可以使用delete删除map中的元素。
我们还可以使用loop遍历map中的所有元素。
package main
import (
"fmt"
)
func main() {
languages := make(map[string]string)
languages["JS"] = "Javascript"
languages["RB"] = "Ruby"
languages["PY"] = "Python"
fmt.Println("List of all languages: ", languages)
fmt.Println("JS shorts for: ", languages["JS"])
delete(languages, "RB")
fmt.Println("List of all languages: ", languages)
for key, value := range languages {
fmt.Printf("For key %v, value is %vn", key, value)
}
}
输出为:
List of all languages: map[JS:Javascript PY:Python RB:Ruby]
JS shorts for: Javascript
List of all languages: map[JS:Javascript PY:Python]
For key JS, value is Javascript
For key PY, value is Python