前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >go的map的多维应用

go的map的多维应用

作者头像
公众号-利志分享
发布2022-04-25 08:25:21
5380
发布2022-04-25 08:25:21
举报
文章被收录于专栏:利志分享

package main import ( "encoding/json" "fmt" ) func main() { //组合二维等长map,生成等长json 例如:{"people1":{"name":"zengzhihai"},"people2":{"name":"liudehua"}} //创建两个2位map,一个二维map,二维map只接受为map的值 var mapList1 = make(map[string]string) var mapList2 = make(map[string]string) var mapList12 = make(map[string]map[string]string) mapList1["name"] = "zengzhihai" mapList2["name"] = "liudehua" //这里是二维map接受两个基本map的值 mapList12["people2"] = mapList2 mapList12["people1"] = mapList1 json12, _ := json.Marshal(mapList12) fmt.Println(string(json12)) //组合3维或者多维的map的json。例如:{"result":1,"data":{"list":[{"titltid":"807242","title":"屌丝男士-饿了吃肉"},{"titltid":"8073342","title":"屌丝男士-波多来了"}],"page":1,"limit":30,"count":1,"total":1}} //这里是一个非常复杂的例子,当然我们主要是讲如何使用map var mapTlist1 = make(map[string]string) mapTlist1 = map[string]string{"titltid": "807242", "title": "屌丝男士-饿了吃肉"} var mapTlist2 = make(map[string]string) mapTlist2 = map[string]string{"titltid": "8073342", "title": "屌丝男士-波多来了"} var mapTlist3 = make([]interface{}, 0) var mapTlist4 = make(map[string]interface{}) var mapTlistLast = make(map[string]interface{}) mapTlist3 = append(mapTlist3, mapTlist1) mapTlist3 = append(mapTlist3, mapTlist2) mapTlist4["page"] = 1 mapTlist4["limit"] = 30 mapTlist4["count"] = 1 mapTlist4["total"] = 1 mapTlist4["list"] = mapTlist3 mapTlistLast["result"] = 1 mapTlistLast["data"] = mapTlist4 fmt.Println(mapTlistLast) jsonLast, _ := json.Marshal(mapTlistLast) fmt.Println(string(jsonLast)) }

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-09-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 利志分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档