Golang 基礎學習之Go map操作

WilliamSheng發表於2017-06-14
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")
}

}

相關文章