How to add data to a map of maps in Golang?
Asked Answered
G

2

24

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.

Gastroscope answered 29/9, 2020 at 22:22 Comment(0)
G
30

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"
Glynda answered 29/9, 2020 at 22:26 Comment(5)
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)?Gastroscope
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.Glynda
@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).Ironic
@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?Gastroscope
The map data is initialized. The map data["someitem"] is not.Glynda
S
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)
Selfexecuting answered 6/10, 2020 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.