Go:将值分配给空映射中的条目

huangapple go评论103阅读模式
英文:

Go : assignment to entry in nil map

问题

在下面的代码中,当尝试给map(countedData)设置值时,我遇到了一个错误,错误信息是assignment to entry in nil map

  1. func receiveWork(out <-chan Work) map[string][]ChartElement {
  2. var countedData map[string][]ChartElement
  3. for el := range out {
  4. countedData[el.Name] = el.Data
  5. }
  6. fmt.Println("This is never executed !!!")
  7. return countedData
  8. }

Println语句没有执行(因为错误发生在它之前的一行)。

有一些goroutine正在向通道发送数据,receiveWork方法应该创建一个如下所示的map:

  1. map =>
  2. "typeOne" =>
  3. [
  4. ChartElement,
  5. ChartElement,
  6. ChartElement,
  7. ],
  8. "typeTwo" =>
  9. [
  10. ChartElement,
  11. ChartElement,
  12. ChartElement,
  13. ]

请帮我修复这个错误。

英文:

When trying to set value to the map(countedData) in the below code, I am getting an error that says, assignment to entry in nil map.

  1. func receiveWork(out &lt;-chan Work) map[string][]ChartElement {
  2. var countedData map[string][]ChartElement
  3. for el := range out {
  4. countedData[el.Name] = el.Data
  5. }
  6. fmt.Println(&quot;This is never executed !!!&quot;)
  7. return countedData
  8. }

Println does not execute (as the error occurs on a lien before that).

There are some goroutines , that are sending data to channel, and receiveWork method should be making a map like this:

  1. map =&gt;
  2. &quot;typeOne&quot; =&gt;
  3. [
  4. ChartElement,
  5. ChartElement,
  6. ChartElement,
  7. ],
  8. &quot;typeTwo&quot; =&gt;
  9. [
  10. ChartElement,
  11. ChartElement,
  12. ChartElement,
  13. ]

Please help me fix the error.

答案1

得分: 37

《Go编程语言规范》

映射类型

使用内置函数make创建一个新的空映射值,该函数接受映射类型和可选的容量提示作为参数:

  1. make(map[string]int)
  2. make(map[string]int, 100)

初始容量并不限制其大小:映射会根据存储在其中的元素数量进行扩容,除了nil映射。nil映射等同于空映射,但不能添加元素。

你写的代码:

  1. var countedData map[string][]ChartElement

相反,要初始化映射,请写成:

  1. countedData := make(map[string][]ChartElement)
英文:

> The Go Programming Language Specification
>
> Map types
>
> A new, empty map value is made using the built-in function make, which
> takes the map type and an optional capacity hint as arguments:
>
> make(map[string]int)
> make(map[string]int, 100)
>
> The initial capacity does not bound its size: maps grow to accommodate
> the number of items stored in them, with the exception of nil maps. A
> nil map is equivalent to an empty map except that no elements may be
> added.

You write:

  1. var countedData map[string][]ChartElement

Instead, to initialize the map, write,

  1. countedData := make(map[string][]ChartElement)

答案2

得分: 1

另一种选择是使用复合字面量:

  1. countedData := map[string][]ChartElement{}

https://golang.org/ref/spec#Composite_literals

英文:

Another option is to use a composite literal:

  1. countedData := map[string][]ChartElement{}

https://golang.org/ref/spec#Composite_literals

huangapple
  • 本文由 发表于 2016年2月13日 19:40:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35379378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定