map操作
2018-08-20 本文已影响0人
次序
package main
import "fmt"
func f1() {
m := map[string]string{ //初始化map
"name": "zhangsan",
"mouse": "luoji",
"cup": "greenCup",
}
for k, v := range m { //遍历map
fmt.Printf("-------k=%v-----v=%v-------\n", k, v)
}
//m2:=make(map[string]int)//定义空的map m2=empty map
//fmt.Printf("---m2=--%v-------\n",m2)
//
//var m3 map[string]int//定义空的map m3=nil
//fmt.Printf("---m3=--%v-------\n",m3)
if mc, ok := m["cup5"]; ok { //map取值,判断是否存在
fmt.Printf(mc)
} else {
fmt.Printf("key does not exits\n")
}
mou, ok := m["mouse"]
fmt.Println(mou, ok)
delete(m, "mouse") //删除map
mou, ok = m["mouse"]
fmt.Println(mou, ok)
}
func main() {
f1()
}