英文:
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
方法的开头添加以下代码:
if info.columns == nil {
info.columns = make(map[string]int)
}
这样,当columns
映射为nil时,它将被初始化为空映射。这样就可以安全地对其进行赋值操作了。
修复后的代码如下所示:
func (info *stateInformation) setColumns(record []string) {
if info.columns == nil {
info.columns = make(map[string]int)
}
for idx, column := range record {
info.columns[column] = idx
}
}
希望这可以帮助到你!如果你还有其他问题,请随时提问。
英文:
I'm new in go lang. I'm trying to read csv file and collecting data.
But after run it I got this error :
panic: assignment to entry in nil map
goroutine 1 [running]:
panic(0x4dedc0, 0xc082002440)
C:/Go/src/runtime/panic.go:464 +0x3f4
main.(*stateInformation).setColumns(0xc08202bd40, 0xc082060000, 0x11, 0x20)
F:/Works/Go/src/examples/state-info/main.go:25 +0xda
main.main()
F:/Works/Go/src/examples/state-info/main.go:69 +0xaea
My code :
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
)
type stateInformation struct {
columns map[string]int
}
type state struct {
id int
name string
abbreviation string
censusRegionName string
}
func (info *stateInformation) setColumns(record []string) {
for idx, column := range record {
info.columns[column] = idx
}
}
func (info *stateInformation) parseState(record []string) (*state, error) {
column := info.columns["id"]
id, err := strconv.Atoi(record[column])
if err != nil {
return nil, err
}
name := record[info.columns["name"]]
abbreviation := record[info.columns["abbreviation"]]
censusRegionName := record[info.columns["census_region_name"]]
return &state{
id: id,
name: name,
abbreviation: abbreviation,
censusRegionName: censusRegionName,
}, nil
}
func main() {
// #1 open a file
f, err := os.Open("state_table.csv")
if err != nil {
log.Fatalln(err)
}
defer f.Close()
stateLookup := map[string]*state{}
info := &stateInformation{}
// #2 parse a csv file
csvReader := csv.NewReader(f)
for rowCount := 0; ; rowCount++ {
record, err := csvReader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatalln(err)
}
if rowCount == 0 {
info.setColumns(record)
} else {
state, err := info.parseState(record)
if err != nil {
log.Fatalln(err)
}
stateLookup[state.abbreviation] = state
}
}
// state-information AL
if len(os.Args) < 2 {
log.Fatalln("expected state abbreviation")
}
abbreviation := os.Args[1]
state, ok := stateLookup[abbreviation]
if !ok {
log.Fatalln("invalid state abbreviation")
}
fmt.Println(`
<html>
<head></head>
<body>
<table>
<tr>
<th>Abbreviation</th>
<th>Name</th>
</tr>`)
fmt.Println(`
<tr>
<td>` + state.abbreviation + `</td>
<td>` + state.name + `</td>
</tr>
`)
fmt.Println(`
</table>
</body>
</html>
`)
}
What's wrong in my code?
答案1
得分: 4
我不知道你想要获得什么,但错误提示显示,在赋值时columns
映射没有column
索引,因此引发了恐慌。
恐慌:在空映射中赋值
要使其正常工作,你需要在开始填充索引之前初始化映射本身。
state := &stateInformation{
columns: make(map[string]int),
}
或者另一种初始化方式:
func (info *stateInformation) setColumns(record []string) {
info.columns = make(map[string]int)
for idx, column := range record {
info.columns[column] = idx
}
}
英文:
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.
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.
state := &stateInformation{
columns: make(map[string]int),
}
Or another way to initialize:
func (info *stateInformation) setColumns(record []string) {
info.columns = make(map[string]int)
for idx, column := range record {
info.columns[column] = idx
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论