1 | Go map 操作: |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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" ) } } |