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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看