首页 > 其他 > 详细

map赋值前要先初始化:assignment to entry in nil map

时间:2018-11-16 16:53:09      阅读:198      评论:0      收藏:0      [点我收藏+]

注意这种map的嵌套的形式,make只初始化了map[string]T部分(T为map[int]int),所以下面的赋值会出现错误:

test := make(map[string]map[int]int)
test["go"][0] = 0 // error
1
2
正确的做法:

test := make(map[string]map[int]int)
test["go"] = make(map[int]int)
test["go"][0] = 0
1
2
3
一个常用的做法:

test := make(map[string]map[int]int)
if test["go"] = nil {
    test["go"] = make(map[int]int)
}
test["go"][0] = 0
原文:https://blog.csdn.net/jason_cuijiahui/article/details/79410471

map赋值前要先初始化:assignment to entry in nil map

原文:https://www.cnblogs.com/nyist-xsk/p/9969922.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!