package main
import "fmt"
func main() {
//声明方式一:
var map1 map[string]string
if map1 == nil{
fmt.Println("map1是个空map")
}
map1 = make(map[string]string,4)
map1["one"] = "java"
map1["two"] = "python"
map1["three"] = "go"
fmt.Println(map1)
//声明方式二: := make() 不限制长度
map2 := make(map[int]string)
map2[1] = "java"
map2[2] = "python"
map2[3] = "go"
fmt.Println(map2)
//声明方式三::= map{key:value,},用于初始化
map3 := map[string]string{
"one":"java",
"two":"python",
"three":"go",
}
fmt.Println(map3)
}
package main
import "fmt"
func main() {
ctiyMap := make(map[string]string)
//添加
ctiyMap["A"] = "a"
ctiyMap["B"] = "b"
ctiyMap["C"] = "c"
//遍历
printMap(ctiyMap)
//修改
ctiyMap["B"] = "d"
printMap(ctiyMap)
//删除
delete(ctiyMap,"C")
printMap(ctiyMap)
}
func printMap(ctiyMap map[string]string){
//引用传递
for key, value := range ctiyMap {
fmt.Println("key=",key,",value=",value)
}
}