doudundian9558 2015-03-05 13:41
浏览 47
已采纳

GO语言如何改变地图指针内对象的值

How can I do it?

I have the list of objects, I want list all and change the name of object. I have the list and I'm doing a while end send to another function, there I change the name, but the name doesn't save.

Any idea how can I do it?

https://play.golang.org/p/el3FtwC-3U

And if there is any book that I can read to learn more, please. Thank for helping me =D

  • 写回答

2条回答 默认 最新

  • douju7503 2015-03-05 14:08
    关注

    In the range loop:

    for _, track := range tracks {
        // send track to channel to change the name
        Working(&track, &c)
    }
    

    the track variable is actually a copy of the value contained in the map, because the assignement here works on the Track value type, and in Go, values are copied when assigned.

    What you should do instead is use the key of your map and assign the values from within the loop.

    for key := range tracks {
        t := tracks[key]
        // send track to channel to change the name
        Working(&t, &c)
        tracks[key] = t
    }
    

    See https://play.golang.org/p/9naDP3SfNh

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 找辅导 初学者 想实现一个项目 没有方向
  • ¥15 关于渗漏场的电场分布模拟
  • ¥24 matlab怎么修改仿真初始时间
  • ¥15 两分段线性回归模型分析阈值效应
  • ¥15 前端和后端代码都没报错,但是点登录没反应的?
  • ¥100 需要远程解决QSQLITE问题!
  • ¥15 利用光场表达式画出初始光场强度分布图像等几个问题在这两个图片里
  • ¥15 gozero求手把手教学,400一天
  • ¥15 泥浆冲清水的泥浆分布
  • ¥15 LASSO回归分析筛选关键基因,适合多大样本量?