Assignment to Entry in Nil Map
Lastmod: 2023-01-26

Overview

Example error:

$ go run main.go
panic: assignment to entry in nil map

This panic occurs when you fail to initialize a map properly.

Initial Steps Overview

  1. Check the declaration of the map

Detailed Steps

1) Check the declaration of the map

If necessary, use the error information to locate the map causing the issue, then find where this map is first declared, which may be as below:

func main() {
	var agesMap map[string]int

	agesMap["Amanda"] = 25

	fmt.Println(agesMap["Amanda"])
}

The block of code above specifies the kind of map we want (string: int), but doesn’t actually create a map for us to use. This will cause a panic when we try to assign values to the map. Instead you should use the make keyword as outlined in Solution A. If you are trying to create a series of nested maps (a map similar to a JSON structure, for example), see Solution B.

Solutions List

A) Use ‘make’ to initialize the map

B) Nested maps

Solutions Detail

A) Use ‘make’ to initialize the map

func main() {
	var agesMap = make(map[string]int)

	agesMap["Amanda"] = 25

	fmt.Println(agesMap["Amanda"])
}

Instead, we can use make to initialize a map of the specified type. We’re then free to set and retrieve key:value pairs in the map as usual.

B) Nested Maps

If you are trying to use a map within another map, for example when building JSON-like data, things can become more complicated, but the same principles remain in that make is required to initialize a map.

func main() {
	myMap := make(map[string]map[string]int)

	myMap["Mammals"] = make(map[string]int)

	myMap["Mammals"]["Cows"] = 10
	myMap["Mammals"]["Dogs"] = 2

	fmt.Println(myMap["Mammals"])
}
$ go run main.go
map[Cows:10 Dogs:2]

For a more convenient way to work with this kind of nested structure see Further Step 1. It may also be worth considering using Go structs or the Go JSON package.

Further Steps

  1. Use composite literals to create map in-line

1) Use composite literals to create map in-line

Using a composite literal we can skip having to use the make keyword and reduce the required number of lines of code.

func main() {
	myMap := map[string]map[string]int{
		"Reptiles": {
			"Geckos":          5,
			"Bearded Dragons": 2,
		},
		"Amphibians": {
			"Tree Frogs":  10,
			"Salamanders": 4,
		},
	}
	fmt.Println(myMap)
}

Further Information

https://yourbasic.org/golang/gotcha-assignment-entry-nil-map/ https://stackoverflow.com/questions/35379378/go-assignment-to-entry-in-nil-map https://stackoverflow.com/questions/27267900/runtime-error-assignment-to-entry-in-nil-map

Owner

Joey

comments powered by Disqus