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") } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。