dongwenghe2416 2016-11-10 04:43
浏览 38
已采纳

golang结构的另一个func更改映射

I'm a new one on go.Now I have a question about functoin pass variable.Here is the code:

type User struct {
    Name string
    Map  map[string]string
}
func main() {
    u := User{Name: "Leto"}
    u.Map = make(map[string]string)
    fmt.Println("before --------")
    fmt.Println(unsafe.Pointer(&u))
    fmt.Println(unsafe.Pointer(&(u.Map)))
    fmt.Println(u)
    Modify(u)
    fmt.Println("after --------")
    fmt.Println(u)
}
func Modify(u User) {
    fmt.Println("in func --------")
    fmt.Println(unsafe.Pointer(&u))
    fmt.Println(unsafe.Pointer(&(u.Map)))
    u.Name = "Paul"
    u.Map["t"] = "t"
}

code above output:

before --------
0xc04203a4c0
0xc04203a4d0
{Leto map[]}
in func --------
0xc04203a500
0xc04203a510
after --------
{Leto map[t:t]}

in Modify func i know user is a copy ,so change name not work is ok ,but why change Map effect out user struct ?

  • 写回答

3条回答 默认 最新

  • du21064 2016-11-10 06:15
    关注

    We need to understand how memory allocation is working here in each call:

      u := User{Name: "Leto"}
    // u is an object of type User
    // after this line memory has been allocated to both the
    // properties u.Name(string) and u.Map(reference)
    // lets say allocated memory address for u.Name starts with x
    // for u.Map it starts with y, and note that u.Map is a reference i.e. 
    // the value contained in it will be a different memory address which
    // will be the starting memory address of the actual map
    // right now the value written at y is nil since it 
    // does not point to any memory address
    u.Map = make(map[string]string)
    // now value of y has been updated to z (which is the 
    // memory address of the beginning of the map initialized 
    // with make call) 
    fmt.Println("before --------")
    fmt.Println(unsafe.Pointer(&u))
    fmt.Println(unsafe.Pointer(&(u.Map)))
    fmt.Println(u)
    // here you are about to pass object by value 
    // so basically a new object will be created of type User
    // lets talk about how copy of this object will be created
    // copy of u.Name will have a new address 
    // lets call it x1 and the value "Leto" too will be 
    // copied to memory address starting with x1
    // copy of u.Map will have a new address too lets call it 
    // y1 and its value z will be copied too to the memory address y1
    // I think you must have got your answer by now. 
    // Basically the newly copied object's property u.Map and 
    // the old one's u.Map both points to the same memory address "z" 
    // and hence whosoever updates the map the other one will see it
    Modify(u)
    fmt.Println("after --------")
    fmt.Println(u)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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