Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested maps in Golang

func main() {     var data = map[string]string{}     data["a"] = "x"     data["b"] = "x"     data["c"] = "x"     fmt.Println(data) } 

It runs.

func main() {     var data = map[string][]string{}     data["a"] = append(data["a"], "x")     data["b"] = append(data["b"], "x")     data["c"] = append(data["c"], "x")     fmt.Println(data) } 

It also runs.

func main() {     var w = map[string]string{}     var data = map[string]map[string]string{}     w["w"] = "x"     data["a"] = w     data["b"] = w     data["c"] = w     fmt.Println(data) } 

It runs again!

func main() {     var data = map[string]map[string]string{}     data["a"]["w"] = "x"     data["b"]["w"] = "x"     data["c"]["w"] = "x"     fmt.Println(data) } 

But it fails!?

Is there a problem with nested maps in Go? Or is there no multiple bracket support for nested maps?

like image 763
Özgür Yalçın Avatar asked Jun 01 '17 10:06

Özgür Yalçın


People also ask

How do I create a nested map in Golang?

Golang Create Nested MapWe start by setting the data type of the key (top-level map) and the type of the value. Since this is a nested map, the value of the top-level map is a map. The previous code creates a simple restaurant menu using nested maps. In the first map, we set the data type as an int.

What is nested map?

Nested Maps are those in which values in key:value pairs of the outer map are also maps. In other words maps inside a map, hence nested maps.


2 Answers

The zero value for map types is nil. It is not yet initialized. You cannot store values in a nil map, that's a runtime panic.

In your last example you initialize the (outer) data map, but it has no entries. When you index it like data["a"], since there is no entry with "a" key in it yet, indexing it returns the zero value of the value type which is nil for maps. So attempting to assign to data["a"]["w"] is a runtime panic.

You have to initialize a map first before storing elements in it, for example:

var data = map[string]map[string]string{}  data["a"] = map[string]string{} data["b"] = make(map[string]string) data["c"] = make(map[string]string)  data["a"]["w"] = "x" data["b"]["w"] = "x" data["c"]["w"] = "x" fmt.Println(data) 

Output (try it on the Go Playground):

map[a:map[w:x] b:map[w:x] c:map[w:x]] 

Note that when you declare a variable of map type and initialize it with a composite literal (as in var data = map[string]string{}), that also counts as initializing.

Note that you may also initialize your nested maps with a composite literal:

var data = map[string]map[string]string{     "a": map[string]string{},     "b": map[string]string{},     "c": map[string]string{}, }  data["a"]["w"] = "x" data["b"]["w"] = "x" data["c"]["w"] = "x" fmt.Println(data) 

Output is the same. Try it on the Go Playground.

like image 199
icza Avatar answered Nov 17 '22 23:11

icza


While the most straightforward answer to this question is to initialize your nested maps as previously described, there is another potential option depending on your access pattern. If you need a truly hierarchical system of maps, then the previous answers are just fine. However, if you simply need to look up values in the map using multiple facets, read on!

It is totally acceptable for maps to use structs as keys (in fact, anything that is comparable can be used). Thus, you can use a single map with struct keys like this example from the Golang blog, which is a hit counter that tracks page hits by country:

type Key struct {   Path, Country string }  hits := make(map[Key]int)  // set: Vietnamese person visiting the home page hits[Key{"/", "vn"}]++  // get: see how many Chinese persons read the spec n := hits[Key{"/ref/spec", "cn"}] 

I don't see maps like this often enough, instead many people reach for the nested variant first, which I think may not always be the right fit.

like image 35
Dominic Barnes Avatar answered Nov 18 '22 01:11

Dominic Barnes