1. Api接口传过来的int字段,接收参数后为float64
2. 类似以下这种二维结构,需要单独make子结构 ,eg:
userBindInfo := make(map[string]map[string]interface{})
//tel is bind
userBindInfo["tel"] = make(map[string]interface{})
userBindInfo["tel"]["isbind"] = 1
userBindInfo["tel"]["name"] = "155****1234"
//wechat is bind
userBindInfo["wx"] = make(map[string]interface{})
userBindInfo["wx"]["isbind"] = 0
userBindInfo["wx"]["name"] = ""
3. for range 插入map[int]map[string]interface{}中被覆盖问题:
1. 被覆盖情况:
示例代码:
memList := make(map[int]map[string]interface{})
memI := 0
memData := make(map[string]interface{})
for _, memId := range memIdList {
memIdStr := util.Int64ToString(memId)
baseInfo := userSV.GetBaseInfo(memIdStr)
myGroupInfo := self.GetGroupMemInfo(memIdStr, groupIdInt)
memData["uid"] = memId
memData["uname"] = baseInfo.Uname
memData["avatar"] = baseInfo.Avatar
memData["sex"] = baseInfo.Sex
memData["id"] = myGroupInfo.Db_id
memList[memI] = memData
memI++
}
运行结果(memList):
map[
0:map[id:53 uid:45 uname:getgetsetset5 sex:1]
1:map[id:53 uid:45 uname:getgetsetset5 sex:1]
2:map[id:53 uid:45 uname:getgetsetset5 sex:1]
3:map[id:53 uid:45 uname:getgetsetset5 sex:1]
]
2. 问题解决:
示例代码:
memList := make(map[int]map[string]interface{})
memI := 0
for _, memId := range memIdList {
memIdStr := util.Int64ToString(memId)
baseInfo := userSV.GetBaseInfo(memIdStr)
myGroupInfo := self.GetGroupMemInfo(memIdStr, groupIdInt)
memData := make(map[string]interface{})
memData["uid"] = memId
memData["uname"] = baseInfo.Uname
memData["avatar"] = baseInfo.Avatar
memData["sex"] = baseInfo.Sex
memData["id"] = myGroupInfo.Db_id
memList[memI] = memData
memI++
}
运行结果(memList):
map[
0:map[id:50 uid:42 uname: test001 sex:1]
1:map[id:51 uid:50 uname: test002 sex:1]
2:map[id:52 uid:43 uname: test003 sex:1]
3:map[id:53 uid:45 uname: test004 sex:1]
]
3. 循环处理数据
对于[]string 、[]int等类型的数组结构,循环处理时,需要注意以下两点:
1. 循环方式有以下两种
假定datalist为[]string数组,则
1. for range循环:
for k,v := range datalist{
fmt.Println(k,v)
}
2. 常见for循环:
for i:=0;i<len(datalist);i++{
fmt.Println(datalist[i])
}
2. 区别:
一般很多新手为了开发简单,直接使用for range进行循环,然而,for range时无序的,在循环时,数据输出的顺序并非按照数组中已有排序进行的;因此,对于排序有要求的业务中,千万记得别用for range。使用2中常见for循环方式,可保证数据时定序的( 数组长度使用len(datalist) 获取 )。