Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare recursive/multidimensional map

Tags:

go

type Table struct {
    input map[string]map[string]
}

How to declare a multidimensional or recursive map?

input["var1"]["var2"] = "something"

update

type Table struct {
    input map[map[string]]
}

returns error

 syntax error: unexpected ]
like image 600
clarkk Avatar asked Apr 14 '15 21:04

clarkk


1 Answers

You're looking for map[string]map[string]string. The problem with your example is it's missing the type of the value in the inner map. Here's an example of how to init such a structure;

m := map[string]map[string]string{
        "a": map[string]string{
            "1":"A",
            "2": "B",
        },
        "b": map[string]string{
            "1": "C",
            "2": "D",
        },
    }

Or following your example;

func main() {
        m := map[string]map[string]string{}
        m["var1"] = map[string]string{}
        m["var1"]["var2"] = "something"
        fmt.Println(m["var1"]["var2"])
}

That prints something.

like image 68
evanmcdonnal Avatar answered Oct 07 '22 20:10

evanmcdonnal