27

I am learning to use the map of maps. In the following example, there are three nested maps.

package main

import (
    "fmt"
)

func main() {

    var data = map[string]map[string]map[string]string{}

    data["Date_1"] = map[string]map[string]string{}
    data["Date_1"] = make(map[string]map[string]string, 1)
    data["Date_1"] = make(map[string]map[string]string, 0)

    data["Date_1"]["Sistem_A"] = map[string]string{}
    data["Date_1"]["Sistem_A"] = make(map[string]string, 0)
    data["Date_1"]["Sistem_A"] = make(map[string]string, 0)

    data["Date_1"]["Sistem_A"]["command_1"] = "white"
    data["Date_1"]["Sistem_A"]["command_2"] = "blue"
    data["Date_1"]["Sistem_A"]["command_3"] = "red"

    fmt.Println("data: ", data)
}

Output

data:  map[Date_1:map[Sistem_A:map[command_1:white command_2:blue command_3:red]]]

The problem is that if I want to add the values ​​in one step I get a panic: assignment to entry in nil map.

package main

import (
    "fmt"
)

func main() {

    var data = map[string]map[string]map[string]string{}

    data["Date_1"] = map[string]map[string]string{}
    data["Date_1"] = make(map[string]map[string]string, 0)
    data["Date_1"] = make(map[string]map[string]string, 0)

    data["Date_1"]["Sistem_A"] = map[string]string{}
    data["Date_1"]["Sistem_A"] = make(map[string]string, 0)
    data["Date_1"]["Sistem_A"] = make(map[string]string, 0)

    data["Date_1"]["Sistem_A"]["command_1"] = "white"
    data["Date_1"]["Sistem_A"]["command_2"] = "blue"
    data["Date_1"]["Sistem_A"]["command_3"] = "red"

    data["Date_2"]["Sistem_A"]["command_5"] = "violet"

    fmt.Println("data: ", data)
}

Output

panic: assignment to entry in nil map

There is very little guidance information at this point. Could you help me?

Thank you.

2 Answers 2

34

It is here:

    data["Date_2"]["Sistem_A"]["command_5"] = "violet"

The expression data["Date_2"] will return a nil-map. It is never initialized, so looking for the index ["Sistem_A"] panics. Initialize the map first:

    data["Date_2"] = make(map[string]map[string]string)
    data["Date_2"]["Sistem_A"] = make(map[string]string)
    data["Date_2"]["Sistem_A"]["command_5"] = "violet"
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much. To implement this type of maps within a scheme of three combined For-Ranges (where the maps will obtain its values). I should initialize these maps into the loop? Or is there a way to do it before the For-Range (not knowing the amount of data that will be added in it)?
Before setting/getting a value to/from a map, you have to make sure it is initialized. You can lazily initialize it, or you can write a for loop to initialize all. If you know the keys you need at all levels, a for-loop initialization would be more readable.
@Burak Serdar - just a small point: you can't add to a nil map but can read (get) from a nil map, in which case the default type of the map element is returned (and the 2nd return value, if used, is false to indicate that the element is not present).
@BurakSerdar I have the following question. The case I presented generates a panic because the map was not initialized. But in this example, I see that the map of maps is not null. `` `` var data = map [string] map [string] map [string] string {} if data == nil { fmt.Println ("data is nil") } else { fmt.Println ("data is not nil") } `` `` Output `` `` data is not nil `` `` Is the problem in the nested maps? Are these the ones that not initialized?
The map data is initialized. The map data["someitem"] is not.
2

Your problem is, that you never initialized data["Date_2"] (so it is nil).

So by doing data["Date_2"]["Sistem_A"]["command_5"] = "violet", looking for the index panics.

you have to initialize first as follows:

data["Date_2"]=make(map[string]map[string]string)
data["Date_2"]["Sistem_A"]=make(map[string]string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.