Nested Maps in Go

I recently had to use a bunch of nested maps in Go and struggled way too long to get them “working”. And obviously there wasn’t a lot of good information to be found via $popular_search_engine. So i thought this might make a good blog post.

What gave me the proper nudge in the correct direction of thinking correctly was a Google Groups posting from 2012: https://groups.google.com/g/golang-nuts/c/PoRkoN84KKU/m/7330L9NulHQJ

You need to actually initialize every inner map seperatly if you want to use it! For more details you should read the rest ;-)

A simple example and the attempt of an explaination

Lets create a simple example to demonstrate the issue at hand:

Now open nested_maps.go in your favorite editor and paste in the following piece of code:

What we try to do here:

is to put a JSON array, where each array element contains three key-value pairs that define a group name, the persons name and their age, into a nested map. The intention for this might be that we think processing the data via a nested map is nice. Or whatever.

While this compiles perfectly fine, running the created binary will result in this runtime error:

My n00bish reaction at first was “WTF? I created the map with make() . It’s not nil!” But obviously i was terribly wrong.

What happens here is that Go creates the outer map but nothing else. And this makes perfect sense since it can’t possibly know how many inner maps we might want later on. It’s our job to take care of that.

A solution for that is extending our for loop like this:

The comments hopefully speak for themselves, but anyways: What we added is a simple check if the inner map to which we want write data already exists. This is done with a simple map lookup, which in Go returns not only the potential value but also a boolean value on which we can do a simple check with if . So if we get a false back, we just create the inner map with a make() and all is good.

Here is the full example which executes without a runtime error:

Hope this helps somebody else at some point in the future :-)

IMAGES

  1. 【Golang】Assignment to entry in nil map

    golang assignment to entry in nil map nested map

  2. Assignment To Entry In Nil Map

    golang assignment to entry in nil map nested map

  3. Golang Nested Map of Maps

    golang assignment to entry in nil map nested map

  4. Assignment To Entry In Nil Map

    golang assignment to entry in nil map nested map

  5. Learn Maps in Golang (with examples)

    golang assignment to entry in nil map nested map

  6. Golang Maps example

    golang assignment to entry in nil map nested map

VIDEO

  1. craflend new map

  2. NIL education program (assignment 5d)

  3. Assignment if and nested if

  4. Item asylum Nil map (rare easter egg)

  5. pou's basics 2.5.0 nil.map

  6. ConstantiamPE: Nested & Map art collection

COMMENTS

  1. How to create object map of map (inside map )

    Assigning to entries in a nil map will give you a panic. In your case you probably need to make () the nested maps when they don’t exist. mapData := make (map [string]map [string]int) ... inner, ok := mapData ["foo"] if !ok { inner = make (map [string]int) mapData ["foo"] = inner } inner ["bar"] = 42. vamc ([email protected]) September ...

  2. Nested Maps in Go

    A simple example and the attempt of an explaination. Lets create a simple example to demonstrate the issue at hand: $ go mod init nested_maps. go: creating new go.mod: module nested_maps. $ touch nested_maps.go. Now open nested_maps.go in your favorite editor and paste in the following piece of code: package main import (. "log" "encoding/json ...

  3. go

    2 Answers. Sorted by: 4. Your data: var data = map[string]interface{}{} is a map from string to interface {} so data ["info"] is just an interface {} until you assert its type as map [string]string: data["info"].(map[string]string) // Now you have a map[string]string. Once you have a map [string]string, you can index it as desired: