2017-08-12 74 views

回答

1

可變mikeuser類型的變量而lisa是一個指針,類型是*user

表達式&user{…}表示取指針指向新的用戶對象

+0

如果有一個函數:func(u user)notify {......},'mike.notify()'和'lisa.notify()'和'(* lisa).notify()'是相同? – luoyhang003

+1

@ luoyhang003是的,「自動解引用」:https://stackoverflow.com/a/13533822/3861083 –

0

初始化實例用戶結構沒有區別。
它在目標變量中的訪問方式有很大差異。

  1. 案例1,它複製用戶結構值到麥克風可變
  2. 情況2中,它是指使用麗薩變量的用戶結構值

看這個片段:

type user struct { 
    name string 
    email string 
} 

someuser := user{"mike", "[email protected]"} 
mike := someuser 
lisa := &someuser 

someuser.name = "hello" 
fmt.Println(mike.name) //name is not changed since user struct is copied 
fmt.Println(lisa.name) //name is changed since user struct is referred 
相關問題