在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
概述 //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", } fmt.Println("p6 :", p6) }
//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) }
//demo_16.go package main import ( "fmt" ) func main() { 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) }
|
请发表评论