注意这种map的嵌套的形式,make只初始化了map[string]T部分(T为map[int]int),所以下面的赋值会出现错误:
test := make(map[string]map[int]int) test["go"][0] = 0 // error
正确的做法:
test := make(map[string]map[int]int) test["go"] = make(map[int]int) test["go"][0] = 0
一个常用的做法:
test := make(map[string]map[int]int) if test["go"] == nil {
test["go"] = make(map[int]int) } test["go"][0] = 0
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/212086.html原文链接:https://javaforall.net
