dongmi5607 2017-06-11 17:34
浏览 29
已采纳

传递值与通过引用传递时映射的奇怪突变(Golang)

In the first case I pass a map to by value: package main

import (
    "fmt"
    "time"
)

func timeMap(z map[string]interface{}) {
    z["updated_at"] = time.Now()
}

func main() {
    foo := map[string]interface{}{
        "Matt": 42,
    }
    timeMap(foo)
    fmt.Println(foo)
}

The output is a muted map:

map[updated_at:2009-11-10 23:00:00 +0000 UTC Matt:42]

In the second case the code is almost identical but for passing by reference:

package main

import (
    "fmt"
    "time"
)

func timeMap(z *map[string]interface{}) {
    (*z)["updated_at"] = time.Now()
}

func main() {
    foo := map[string]interface{}{
        "Matt": 42,
    }
    timeMap(&foo)
    fmt.Println(foo)
}

Obviously, the result differs:

map[Matt:42 updated_at:2009-11-10 23:00:00 +0000 UTC]

My expectations were the following:

  • when passing by value map is not muted
  • When passing by reference map is muted like in the second case. However, in the first case map was muted but in the reverse order (compared to the second case).

Why does it happen so?

  • 写回答

1条回答 默认 最新

  • dsvcqvp139098 2017-06-11 17:45
    关注

    There is no such thing as passing by reference in Go. Whenever you pass anything (pointer, slice header, map) it is always passed by value. The question is what exactly is being passed by value (i.e. what is the actual value of the type).

    When u pass a map, you pass a copy of the pointer to its header, which contains a set pointers to the buckets, as in the implementation of the HashTable. https://github.com/golang/go/blob/master/src/runtime/hashmap.go#L106

    Therefore it rarely makes sense to pass a pointer to the map, because the operation of copying a map header pointer is extremely cheap.

    Now why the order is different, this is simply due to internal implementation of the map, ranging over the keys occurs in a random fashion. Again this is just an implementation details.

    EDIT:

    As @icza correctly pointed out, passing a map is actually passing a copy of a pointer to the map header, not the map header itself. Sorry for the confusion

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决