当尝试在下面的代码中将值设置为map
(countedData
)时,出现了一条错误消息assignment to entry in nil map
。
func receiveWork(out <-chan Work) map[string][]ChartElement {
var countedData map[string][]ChartElement
for el := range out {
countedData[el.Name] = el.Data
}
fmt.Println("This is never executed !!!")
return countedData
}
Println
不执行(因为在此之前留置权发生错误)。有一些goroutines将数据发送到channel,
receiveWork
方法应该制作这样的映射:map =>
"typeOne" =>
[
ChartElement,
ChartElement,
ChartElement,
],
"typeTwo" =>
[
ChartElement,
ChartElement,
ChartElement,
]
请帮助我修复错误。
最佳答案
你写:
var countedData map[string][]ChartElement
相反,要初始化 map ,请写,
countedData := make(map[string][]ChartElement)