Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime error: "assignment to entry in nil map"

Tags:

go

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)
like image 758
user387049 Avatar asked Feb 27 '13 16:02

user387049


People also ask

What is a nil map?

A map maps keys to values. The zero value of a map is nil . A nil map has no keys, nor can keys be added. The make function returns a map of the given type, initialized and ready for use.

What is a nil map in Golang?

Go Maps Zero value of a mapA nil map has no keys nor can keys be added. A nil map behaves like an empty map if read from but causes a runtime panic if written to. var m map[string]string // reading m["foo"] == "" // true.

How do you initialize an empty map in Golang?

Go by Example: Maps To create an empty map, use the builtin make : make(map[key-type]val-type) . Set key/value pairs using typical name[key] = val syntax. Printing a map with e.g. fmt. Println will show all of its key/value pairs.


1 Answers

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])
    }
}
like image 133
thwd Avatar answered Oct 15 '22 16:10

thwd