运行时错误:“分配给nil映射中的条目” [英] Runtime error: "assignment to entry in nil map"

查看:104
本文介绍了运行时错误:“分配给nil映射中的条目”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一部分地图。虽然代码编译得很好,但我得到了下面的运行时错误:

  mapassign1:runtime·panicstring(赋值给nil映射); 

我尝试制作一个Maps数组,每个Map包含两个标记,一个Id和一个投资者。我的代码如下所示:

  for _,row:= range rows {
var inv_ids [] string
var inv_names [] string

//从MySQL创建数据数组GROUP_CONCAT函数
inv_ids = strings.Split(row.Str(10),,)
inv_names = strings.Split(row.Str(11),,)
length:= len(inv_ids);

invs:= make([] map [string] string,length)

//建立ids =为i:= 0命名
;我<长度; i ++ {
invs [i] = make(map [string] string)
invs [i] [Id] = inv_ids [i]
invs [i] [Investor] = inv_names [i]
} //对于

//构建消息并返回
msg:= InfoMessage {row.Int(0),row.Int(1), row.Str(2),row.Int(3),row.Str(4),row.Float(5),row.Float(6),row.Str(7),row.Str(8),row .Int(9),invs}
return(msg)
} // for



我最初认为像下面这样的东西会起作用,但是这并没有解决问题。任何想法?

$ p $ invs:= make([] make(map [string] string),length)


解决方案

您正尝试创建切片 ;考虑下面的例子:



http://play.golang.org / p / gChfTgtmN-

 包主
$ b $导入fmt

func main(){
a:= make([] map [string] int,100)
for i:= 0;我< 100; i ++ {
a [i] = map [string] int {id:i,投资者:i}
}
fmt.Println(a)
}

您可以重写这些行:

  invs [i] = make(map [string] string)
invs [i] [Id] = inv_ids [i]
invs [ ] = inv_names [i]

as:

  invs [i] = map [string] string {Id:inv_ids [i],Investor:inv_names [i]} 

这就是所谓的复合文字



在一个更习惯的程序中,你很可能想用 struct 来表示投资者:

< a href =http://play.golang.org/p/vppK6y-c8g> http://play.golang.org/p/vppK6y-c8g

 包主要

导入(
fmt
strconv


类型投资者结构{
Id int
名称字符串
}

func main(){
a:= make([] Investor,100 )
for i:= 0;我< 100; i ++ {
a [i] = Investor {Id:i,Name:John+ strconv.Itoa(i)}
fmt.Printf(%#v \ n,a [i] )
}
}


I'm trying to create a slice of Maps. Although the code compiles fine, I get the runtime error below:

mapassign1: runtime·panicstring("assignment to entry in nil map");

I attempt to make an array of Maps, with each Map containing two indicies, a "Id" and a "Investor". My code looks like this:

for _, row := range rows {
        var inv_ids []string
        var inv_names []string

        //create arrays of data from MySQLs GROUP_CONCAT function
        inv_ids = strings.Split(row.Str(10), ",")
        inv_names = strings.Split(row.Str(11), ",")
        length := len(inv_ids);

        invs := make([]map[string]string, length)

        //build map of ids => names
        for i := 0; i < length; i++ {
            invs[i] = make(map[string]string)
            invs[i]["Id"] = inv_ids[i]
            invs[i]["Investor"] = inv_names[i]
        }//for

        //build Message and return
        msg := InfoMessage{row.Int(0), row.Int(1), row.Str(2), row.Int(3), row.Str(4), row.Float(5), row.Float(6), row.Str(7), row.Str(8), row.Int(9), invs}
        return(msg)
    } //for

I initially thought something like below would work, however that did not fix the issue either. Any ideas?

invs := make([]make(map[string]string), length)

解决方案

You are trying to create a slice of maps; consider the following example:

http://play.golang.org/p/gChfTgtmN-

package main

import "fmt"

func main() {
    a := make([]map[string]int, 100)
    for i := 0; i < 100; i++ {
        a[i] = map[string]int{"id": i, "investor": i}
    }
    fmt.Println(a)
}

You can rewrite these lines:

invs[i] = make(map[string]string)
invs[i]["Id"] = inv_ids[i]
invs[i]["Investor"] = inv_names[i]

as:

invs[i] = map[string]string{"Id": inv_ids[i], "Investor": inv_names[i]}

this is called a composite literal.

Now, in a more idiomatic program, you'd most probably want to use a struct to represent an investor:

http://play.golang.org/p/vppK6y-c8g

package main

import (
    "fmt"
    "strconv"
)

type Investor struct {
    Id   int
    Name string
}

func main() {
    a := make([]Investor, 100)
    for i := 0; i < 100; i++ {
        a[i] = Investor{Id: i, Name: "John" + strconv.Itoa(i)}
        fmt.Printf("%#v\n", a[i])
    }
}

这篇关于运行时错误:“分配给nil映射中的条目”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆