dongliu6848 2014-04-14 16:09
浏览 38
已采纳

如何在golang中创建关联地图?

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 
", superMap["user"])

    cnt := len(superMap["user"])
    if cnt > 1{


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

Play

  • 写回答

1条回答 默认 最新

  • dscojuxf69080 2014-04-14 17:40
    关注

    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 
    ", superMap["user"])
        cnt := len(superMap["user"])
    
        if cnt > 1 {
            fmt.Printf("expected only one value received %v", cnt)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?