Go运行时错误:“对nil映射中的条目进行赋值”

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

Go Runtime error: “assignment to entry in nil map”

问题

你的代码中出现了一个错误。错误信息是"panic: assignment to entry in nil map",意味着在nil映射中进行了赋值操作。

具体来说,问题出现在stateInformation结构体的columns字段上。在setColumns方法中,你尝试将值赋给info.columns[column],但是columns映射是nil的,没有被初始化。

为了解决这个问题,你需要在stateInformation结构体的setColumns方法中初始化columns映射。你可以在setColumns方法的开头添加以下代码:

  1. if info.columns == nil {
  2. info.columns = make(map[string]int)
  3. }

这样,当columns映射为nil时,它将被初始化为空映射。这样就可以安全地对其进行赋值操作了。

修复后的代码如下所示:

  1. func (info *stateInformation) setColumns(record []string) {
  2. if info.columns == nil {
  3. info.columns = make(map[string]int)
  4. }
  5. for idx, column := range record {
  6. info.columns[column] = idx
  7. }
  8. }

希望这可以帮助到你!如果你还有其他问题,请随时提问。

英文:

I'm new in go lang. I'm trying to read csv file and collecting data.

But after run it I got this error :

  1. panic: assignment to entry in nil map
  2. goroutine 1 [running]:
  3. panic(0x4dedc0, 0xc082002440)
  4. C:/Go/src/runtime/panic.go:464 +0x3f4
  5. main.(*stateInformation).setColumns(0xc08202bd40, 0xc082060000, 0x11, 0x20)
  6. F:/Works/Go/src/examples/state-info/main.go:25 +0xda
  7. main.main()
  8. F:/Works/Go/src/examples/state-info/main.go:69 +0xaea

My code :

  1. package main
  2. import (
  3. "encoding/csv"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "strconv"
  9. )
  10. type stateInformation struct {
  11. columns map[string]int
  12. }
  13. type state struct {
  14. id int
  15. name string
  16. abbreviation string
  17. censusRegionName string
  18. }
  19. func (info *stateInformation) setColumns(record []string) {
  20. for idx, column := range record {
  21. info.columns[column] = idx
  22. }
  23. }
  24. func (info *stateInformation) parseState(record []string) (*state, error) {
  25. column := info.columns["id"]
  26. id, err := strconv.Atoi(record[column])
  27. if err != nil {
  28. return nil, err
  29. }
  30. name := record[info.columns["name"]]
  31. abbreviation := record[info.columns["abbreviation"]]
  32. censusRegionName := record[info.columns["census_region_name"]]
  33. return &state{
  34. id: id,
  35. name: name,
  36. abbreviation: abbreviation,
  37. censusRegionName: censusRegionName,
  38. }, nil
  39. }
  40. func main() {
  41. // #1 open a file
  42. f, err := os.Open("state_table.csv")
  43. if err != nil {
  44. log.Fatalln(err)
  45. }
  46. defer f.Close()
  47. stateLookup := map[string]*state{}
  48. info := &stateInformation{}
  49. // #2 parse a csv file
  50. csvReader := csv.NewReader(f)
  51. for rowCount := 0; ; rowCount++ {
  52. record, err := csvReader.Read()
  53. if err == io.EOF {
  54. break
  55. } else if err != nil {
  56. log.Fatalln(err)
  57. }
  58. if rowCount == 0 {
  59. info.setColumns(record)
  60. } else {
  61. state, err := info.parseState(record)
  62. if err != nil {
  63. log.Fatalln(err)
  64. }
  65. stateLookup[state.abbreviation] = state
  66. }
  67. }
  68. // state-information AL
  69. if len(os.Args) < 2 {
  70. log.Fatalln("expected state abbreviation")
  71. }
  72. abbreviation := os.Args[1]
  73. state, ok := stateLookup[abbreviation]
  74. if !ok {
  75. log.Fatalln("invalid state abbreviation")
  76. }
  77. fmt.Println(`
  78. <html>
  79. <head></head>
  80. <body>
  81. <table>
  82. <tr>
  83. <th>Abbreviation</th>
  84. <th>Name</th>
  85. </tr>`)
  86. fmt.Println(`
  87. <tr>
  88. <td>` + state.abbreviation + `</td>
  89. <td>` + state.name + `</td>
  90. </tr>
  91. `)
  92. fmt.Println(`
  93. </table>
  94. </body>
  95. </html>
  96. `)
  97. }

What's wrong in my code?

答案1

得分: 4

我不知道你想要获得什么,但错误提示显示,在赋值时columns映射没有column索引,因此引发了恐慌。

  1. 恐慌:在空映射中赋值

要使其正常工作,你需要在开始填充索引之前初始化映射本身。

  1. state := &stateInformation{
  2. columns: make(map[string]int),
  3. }

或者另一种初始化方式:

  1. func (info *stateInformation) setColumns(record []string) {
  2. info.columns = make(map[string]int)
  3. for idx, column := range record {
  4. info.columns[column] = idx
  5. }
  6. }
英文:

I don't know what you are trying to obtain, but the error tells, that columns map does not have a column index on the moment of assignment and for this reason is throwing a panic.

  1. panic: assignment to entry in nil map

To make it work you have to initialize the map itself before to start to populate with indexes.

  1. state := &stateInformation{
  2. columns: make(map[string]int),
  3. }

Or another way to initialize:

  1. func (info *stateInformation) setColumns(record []string) {
  2. info.columns = make(map[string]int)
  3. for idx, column := range record {
  4. info.columns[column] = idx
  5. }
  6. }

huangapple
  • 本文由 发表于 2016年3月14日 19:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35986604.html
匿名

发表评论

匿名网友

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

确定