3 回答

TA貢獻1811條經驗 獲得超6個贊
我們需要了解在每次調用中內存分配是如何工作的:
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)

TA貢獻1757條經驗 獲得超7個贊
- 3 回答
- 0 關注
- 193 瀏覽
添加回答
舉報