How to create object map of map(inside map )

Can any one tell me how to create inside map object…

Ex:- mapData := make(map[string]map[string]int) i try create object inside map but it thows error nil

)

That should make a nested map as you want it, but what do you mean by throwing error nil?

There is nothing thrown in golang and if you get an error == nil returned everything is fine, therefore I’ve some trouble to understand

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

Sorry it s not thows if i assign value in neste map i get run time error assignment to entry in nil map , Create the object of nested map

my code is like this :- mapData := make(map[string]map[string]string)
mapData[“golang”][“org”] = “google”

Okay Thank you

mapData := make(map[string]map[string]int)

inner, ok := mapData[“foo”]
if !ok {
inner = make(map[string]int)
mapData[“foo”] = inner
}
inner[“bar”] = 42

If i add the new key value again it will create new nested map but i dont want to new object only one time i will create the nested map object then we can added value number of times, It is possible in go…please can you tell me

That is what the !ok check does.

1 Like

Okay

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.