Go map 操作:
package main import "fmt" func main() { var countryMap map[string]string /* 创建集合 */ countryMap = make(map[string]string) /* map 插入 key-value 对,各个国家对应的首都 */ countryMap["ShengRI"] = "1988-04-09" /* 使用 key 输出 map 值 */ for country := range countryMap { fmt.Println("Capital of",country,"is",countryMap[country]) } /* 查看元素在集合中是否存在 */ captial, ok := countryMap["ShengRI"] /* 如果 ok 是 true, 则存在,否则不存在 */ if(ok){ fmt.Println("Capital of United States is", captial) }else { fmt.Println("is not ok") } MyMap() } func MyMap(){ var myMap map[string]string myMap = make(map[string]string) myMap["idc_id"] = "2" myMap["idc_name"] = "BeiJingIDC" myMap["idc_prod"] = "OpenStack" myMap["idc_zone"] = "BJ" myMap["idc_abs"] = "2018-10-02" for m := range myMap{ fmt.Println(m,myMap[m]) } delete(myMap,"idc_abs") fmt.Println(myMap) myMap["idc_abs"] = "2088-10-02" fmt.Println(myMap) _, ok := myMap["idc_zone"] if ok { fmt.Println(myMap["idc_zone"],"is ok") } }
时间: 2024-10-05 22:11:36