16

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)
user387049
  • 6,647
  • 8
  • 53
  • 55

1 Answers1

15

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])
    }
}
thwd
  • 23,956
  • 8
  • 74
  • 108
  • 1
    Before I wrote the question I even told myself, make sure you call it a slice not an array haha. I've edited the question, and everything seems to work using the composite literal. Thanks! – user387049 Feb 27 '13 at 17:37