Panic: assignment to entry in nil map

I need help, as upgraded Go into latest build version 1.7, but now wondering if the problem I am getting is because of the update? Where I do not encounter the panic: assignment to entry in nil map error before.

I declare the map as below public variable
var (
droid = make(map[string] map[string] Match)
)

but I am getting below error -
panic: assignment to entry in nil map

Thanks

Hey @frayela,

If you show the code where you are actually using droid and Match, it’ll be easier to help you, but by using the code below for example, I’m not getting any errors, so I’m going to go out on a limb and ask if Match itself contains a map?

package main

type Match bool

var (
	droid = make(map[string]map[string]Match)
)

func main() {
	droid["r2d2"] = map[string]Match{"one": true}
}

I’m guessing (which is all we can do in the absence of actual code) that you’re then doing something like droid["hello"]["banana"] = whatever. While droid is an initialized map, droid["hello"] will be nil unless you’ve assigned something to it, hence you would get the above panic.

Thanks for looking into my query and for trying to understand my problem below is the code snippet -

package match

type Match struct {
Match int
Points int
}

var (
droid = make(map[string] map[string] Match)
)

.
.

func AssessPlayerSet(matchId, droidId) bool {
invalid := false
setResult := “M”
.
.
droid[matchId][droidId] = Match{1, 100} <-- this is line trown the Panic: assignment to entry in nil map

return invalid
}

The above code is being called from main

Hey @frayela, you need to replace that line with the following for it to work:
droid[matchId] = map[string]Match{droidId: Match{1, 100}}

This is saying, initialize the map[string] of a map[string]Match type, to the key matchId and the map to a new map with the key droidId and the value of a Match object.

1 Like

Thanks Benjamin, I have no idea I can do that way it seems working now. I never had such problem before but I am not sure if it is because I have update to latest Go 1.7 Anyway, seems like it is working now. Once again. Thanks.

No worries @frayela, glad to hear it’s working now.

If you were using the same code in Go 1.6, as far as I know it would still have shown that error, but maybe you changed some code somewhere along the way and then it broke…

Either way glad I could help…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.