Golang Map struct in another one

go

Solution

An alternative to using a map inside a map, is to have a single `map[mapKey]` where `mapKey` is a struct:

type mapKey struct {
    Key    string
    Option string
}

The benefits is that you only have to do a single lookup when searching for a `myStruct`, and you only have to create a single map. The downside is in case you need to be able to get that options `map[string]myStruct` map, since it does not exist. Also, you cannot check if a certain key exists or not, because keys and options exists in pairs.

Working example:

package main

import "fmt"

type myStruct struct {
    atrib1 string
    atrib2 string
}

type mapKey struct {
    Key    string
    Option string
}

func main() {
    apiKeyTypeRequest := make(map[mapKey]myStruct)

    apiKeyTypeRequest[mapKey{"Key", "MyFirstOption"}] = myStruct{"first Value first op", "second Value first op"}
    apiKeyTypeRequest[mapKey{"Key", "MysecondtOption"}] = myStruct{atrib1: "first Value second op"}

    fmt.Printf("%+v\n", apiKeyTypeRequest)
}

Playground: http://play.golang.org/p/tGd7ja7QI2

Problem

I'm new working on golang, In this case I have a map [string] which has a struct. At this point everything is works. But I would like to have a map[string] in which I can access at the same time to another map[string] which has it self struct. This is the code in which I'm trying to work. ``` type myStruct struct{ atrib1 string atrib2 string } var apiRequest map[string] map[string]myStruct ``` I would like acces to something like this: ``` func main() { apiRequest = make(map[string] map[string]myStruct) apiKeyTypeRequest["Key"]["MyFirstOption"].atrib1 = "first Value first op" apiKeyTypeRequest["Key"]["MyFirstOption"].atrib2 = "second Value first op" apiKeyTypeRequest["Key"]["MysecondtOption"].atrib1 = "first Value second op" } ```

Original source