How to create an associative map in golang?

arrays, go

Solution

As Arjan said in the comment, you need to move the creation of `aMap` into the for loop. The reason is because, in the original code you posted you are dealing with one instance of `aMap` in memory. That means, only one map called `aMap` is created and when you assign another variable to the value of `aMap` you are assigning a reference. This means, any variable that hold a reference (to `aMap`) where state is mutated, will be observed in all other variables also holding the reference because they all resolve to the same object in memory.

When the `aMap` is moved into the for/range loop, this means that 4 individual instances of `aMap` will be created all with their own memory. Mutating the state of one of those `aMaps` will not affect the others because they are their own objects in memory. Now, if you took one of those objects and made a reference to it again with another variable then you'd end up in the same boat as the first case.

package main

import (
    "fmt"
)

func main() {

    aData := map[int]string{
        0: "apple",
        1: "samsung",
        2: "htc",
        3: "sony",
    }

    dir := []string{"user", "doc", "bin", "src"}

    //aMap := make(map[int]string) //only one map instance is created in memory
    superMap := make(map[string]map[int]string)

    for k, v := range dir {
        //multiple map instances are created in memory
        aMap := make(map[int]string)
        aMap[k] = aData[k]
        superMap[v] = aMap

    }

    hello(superMap)
}

func hello(superMap map[string]map[int]string) {
    fmt.Printf("superMap of user is %v \n", superMap["user"])
    cnt := len(superMap["user"])

    if cnt > 1 {
        fmt.Printf("expected only one value received %v", cnt)
    }
}

Problem

I'm trying to create an 'associative' map. The issue is that the superMap collects all the values of aMap. Basically even if I want to store only one instance of amap on each look I endup storing the current instance plus all the previous loops. `supermap[value] = amap[value]` . However the snippet below stores `supermap[value] = amap`. The reason is that I can't find any way to fix this. If I delete the old values of aMap after each loop they are deleted from `supermap` as well. ``` package main import ( "fmt" ) func main() { aData := map[int]string{ 0: "apple", 1: "samsung", 2: "htc", 3: "sony", } dir := []string{"user", "doc", "bin", "src"} aMap := make(map[int]string) superMap := make(map[string]map[int]string) for k, v := range dir { aMap[k] = aData[k] superMap[v] = aMap } hello(superMap) } func hello(superMap map[string]map[int]string) { fmt.Printf("superMap of user is %v \n", superMap["user"]) cnt := len(superMap["user"]) if cnt > 1{ fmt.Printf("expected only one value received %v", cnt) } } ``` Play

Original source