Nested maps correct manupulation of any type (value as interface)

Hi ppl , hope this Q is not overlapping , searched long before asking.
i wanted to create a dict when the value can be any thing , which is working , but the last 3 lines doesn’t

func main() {
	a := make(map[string]interface{})

	a["start"] = map[string]interface{}{
		"hello": 2,
		"world": 3,
		"here": map[string]interface{}{
			"test1": 123,
			"test2": "dsd",
			"level3": map[string]interface{}{
				"ha1": "kuku",
				"ha2": "nana",
			},
		},
	}
	fmt.Println(a)
	a["start"]["here"]["level3"]["222"] = "string1"                         **// why i got an error ?** 
	a["start"]["here"]["level3"]["444"] = make(map[string]interface{})      **// why i get an error ?** 
	delete(a["start"]["here"], "world")                                     **// why i get an error ?** 
}

The Q is the last 3 lines , will appreciate as much more details as possible.

also i would like to add this little block here :

	dict2 := make(map[interface{}]interface{})
	dict2[1] = "wow"
	dict2["2"] = "wawa"
	dict2[3] = make(map[interface{}]interface{})                    // **this is not working as well** 
	dict2[3]["test3"] = "nice"                                      // **this is not working as well** 

this is same family questions so i have decided to put it here , guess i am missing some go concept.
and there the Q about the last 2 lines.

thanks in advance
Edgar

Hi @edgarlip ,

What error messages do you get for each of the failing lines?

@christophberger - sry for taking me long.

for the first 3 lines :

	a["start"]["here"]["level3"]["222"] = "string1"                        
	a["start"]["here"]["level3"]["444"] = make(map[string]interface{})   
	delete(a["start"]["here"], "world")   

the error :

invalid operation: cannot index a["start"] (map index expression of type interface{})compilerNonIndexableOperand

for the second part , line :

dict2[3]["test3"] = "nice"

the error :

invalid operation: cannot index dict2[3] (map index expression of type interface{})compilerNonIndexableOperand

very similar

You need to type assert for each level of nesting, that the next level is a map[string]interface{}.

2 Likes

but it looks like i did that already, can u please elaborate ?

I don’t see a single type assertion.

https://go.dev/tour/methods/15

@NobbZ - i am not a go expert, and even tho i have read what assert is and why should i use it
still don’t see how can it help me, so please be more informative with the answer you give , like
an example that relates to my code would be great.

I gave you a link that explains type assertions. You need to use them to assert that each level is still a map[string]interface{}.

Pseudo code (as typing on mobile):

if a, ok := m["a"].(map[string]interface{}); ok {
  if b, ok := a["b".(map[string]interface{})]; ok {
    println(b)
  }
}

This is a working version of your initial code:

1 Like

Thank you @NobbZ , very appriciated , but i wonder how do i get to a point that i can work with dictionaries in go in the same comfort that i work with them in python.
is it possible?

As long as they are monomorphic it’s fine, though interface{} is always nasty to deal with.

You can’t do anything with it, unless you type assert first.

1 Like

@NobbZ - can u please give an example of how to work with monomorphic dict as well(that u have
mentione , let’s say :
dict1 := map{string}map{} , and now i want that each level will filled with strings as keys
and the value of the key will be map which will hold a key ( string ) and value of another map
let’s say 4 level deep ( may be less as long the example is clear ), example (yamelish):

                     "edgarNobbZ":
                       "answer1":
                         "answer3":
                           "answer5":
                             "etc":
                       "answer2":
                         "answer4":
                           "answer6":
                             "etc":

A map with string keys on each level and a string value on the fourth level would look like this:

1 Like

also added how to add a key and how to remove a key , now this post looks informative for wondering souls -)

and gain , 10x @NobbZ !

1 Like

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