多维map赋值时,要注意,需要先声明,再赋值,如下:
func TestMap(t *testing.T) { groupDict := make(map[int32]map[string]int32) groupType := int32(1) // 声明 if _, ok := groupDict[groupType]; !ok { groupDict[groupType] = make(map[string]int32) } groupDict[groupType]["test"] = 1 t.Log(groupDict) }
如果没有中间的声明部分,则会就会执行报错:
--- FAIL: TestMap (0.00s) panic: assignment to entry in nil map [recovered] panic: assignment to entry in nil map