24

I have a map which has as value an array of maps.

Example:

 thisMap["coins"][0] = aMap["random":"something"]
 thisMap["notes"][1] = aMap["not-random":"something else"]
 thisMap["coins"][2] = aMap["not-random":"something else"]

I can't figure it out how to do this as go seems to allow setting data only at one level when you deal with maps [name][value] = value.

So far I have this code which fails

package main

func main() {

    something := []string{"coins", "notes", "gold?", "coins", "notes"}

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

    for k, v := range something {
        aMap := map[string]string{
            "random": "something",
        }

        thisMap[v] = [k]aMap
    }
}

Edit: The slice values ("coins", "notes" etc ) can repeat so this is the reason why I need use an index [] .

1
  • 5
    Just a tangential note: you are technically using a slice of maps, not an array of maps. blog.golang.org/slices Commented Apr 14, 2014 at 17:58

1 Answer 1

27

Working example (click to play):

something := []string{"coins", "notes", "gold?"}

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

for _, v := range something {
    aMap := map[string]int{
        "random": 12,
    }

    thisMap[v] = append(thisMap[v], aMap)
}

When iterating over the newly created thisMap, you need to make room for the new value aMap. The builtin function append does that for you when using slices. It makes room and appends the value to the slice.

If you're using more complex data types that can't be initialized as easily as slices, you first have to check if the key is already in the map and, if it is not, initialize your data type. Checking for map elements is documented here. Example with maps (click to play):

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

for _, v := range something {
    if _, ok := thisMap[v]; !ok {
        thisMap[v] = make(map[string]int)
    }
    thisMap[v]["random"] = 12
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Dustin: yep in this case it's true that you don't need to check the key. However, I'd like to keep the answer more general.
In general, I'd never make a zero element slice. I might use a literal in a special case. Not having more code than is required to solve a problem correctly is best practice.
@Dustin: I meant keeping the answer as general as possible if somebody with a similar problem stumbles over it. For slices you can short the code, yes. For maps, for example, you can't. These must be initialized, so you'd need key checking. I'll just reformulate things...
@nemo this doesn't seem to work if the slice has repeating values. I was thinking to use an index/[] exactly due this reason. Should I order them first to avoid loosing some data ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.