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 ?