总体
代码
//demo_14.go
package main
import (
"fmt"
)
func main() {
var p1 map[int]string
p1 = make(map[int]string)
p1[1] = "Tom"
fmt.Println("p1 :", p1)
var p2 map[int]string = map[int]string{}
p2[1] = "Tom"
fmt.Println("p2 :", p2)
var p3 map[int]string = make(map[int]string)
p3[1] = "Tom"
fmt.Println("p3 :", p3)
p4 := map[int]string{}
p4[1] = "Tom"
fmt.Println("p4 :", p4)
p5 := make(map[int]string)
p5[1] = "Tom"
fmt.Println("p5 :", p5)
p6 := map[int]string{
1 : "Tom",
2 : "aaa",
}
fmt.Println("p6 :", p6)
p7 :=map[string]string{
"a":"aaa",
}
fmt.Println("p7 :", p7)
}
输出
p1 : map[1:Tom]
p2 : map[1:Tom]
p3 : map[1:Tom]
p4 : map[1:Tom]
p5 : map[1:Tom]
p6 : map[1:Tom 2:aaa]
p7 : map[a:aaa]
高级使用
//demo_15.go
package main
import (
"encoding/json"
"fmt"
)
func main() {
res := make(map[string]interface{})
res["code"] = 200
res["msg"] = "success"
res["data"] = map[string]interface{}{
"username" : "Tom",
"age" : "30",
"hobby" : []string{"读书","爬山"},
}
fmt.Println("map data :", res)
//序列化
jsons, errs := json.Marshal(res)
if errs != nil {
fmt.Println("json marshal error:", errs)
}
fmt.Println("")
fmt.Println("--- map to json ---")
fmt.Println("json data :", string(jsons))
//反序列化
res2 := make(map[string]interface{})
errs = json.Unmarshal([]byte(jsons), &res2)
if errs != nil {
fmt.Println("json marshal error:", errs)
}
fmt.Println("")
fmt.Println("--- json to map ---")
fmt.Println("map data :", res2)
}
基础用法
//删除
person := map[int]string{
1 : "Tom",
2 : "Aaron",
3 : "John",
}
fmt.Println("data :",person)
delete(person, 2)
fmt.Println("data :",person)
person[2] = "Jack"
person[3] = "Kevin"
fmt.Println("data :",person)