Peter Bengtsson
Peter Bengtsson

Reputation: 7485

map of map of structs and "assignment to entry in nil map"

Here's an example where I get assignment to entry in nil map every time: https://play.golang.org/p/LudJs0rVbs

To demonstrate what I'm trying to do, here's a naive version that causes 2 lookups to the database (you'll have to use your imagination on line 11): https://play.golang.org/p/YZNFeMHyMs

Basically, I'm trying to do this:

things := make(map[string]map[string][]Struct)
...
stuff, there := things["first key"]
if !there {
    things["first key"] = getAMapOfStringToStructs()
}
doSomethingWith(things["first key"])

I've looked at the more trivial examples of maps-of-maps here but I can't seem to map that to my problem.

Upvotes: 2

Views: 2626

Answers (1)

dave
dave

Reputation: 64657

You never did make on your allEntries map:

allEntries = make(map[string]map[string][]Thing)

https://play.golang.org/p/ecdUU30FQT

Upvotes: 2

Related Questions