go map


package main

import "fmt"

func main() {
var countryCapitalMap map[string]string
countryCapitalMap = make(map[string]string)

/* map插入key - value对,各个国家对应的首都 */
countryCapitalMap [ "France" ] = "Paris"
countryCapitalMap [ "Italy" ] = "罗马"
countryCapitalMap [ "Japan" ] = "东京"
countryCapitalMap [ "India " ] = "新德里"

for country := range countryCapitalMap {
fmt.Println(country,"首都是",countryCapitalMap[country])
}

capital,ok := countryCapitalMap ["美国"]

if (ok) {
fmt.Println("美国的首都是",capital)
}else {
fmt.Println("美国的首都是22333",capital)

}

delete(countryCapitalMap,"France")
fmt.Println("删除元素后地图")

/*打印地图*/
for country := range countryCapitalMap {
fmt.Println(country, "首都是", countryCapitalMap [ country ])
}


}



Loading Disqus comments...
Table of Contents