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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度