type animal struct{ sound string}func (a *animal) bark(s string) { (*a).sound = s}var yourAnimal *animal //yourAnimal is an address so this makes sense i.e. the receiver expects an address since it is of type *animal:yourAnimal.bark("woof")fmt.Println(*yourAnimal)//But why does this prints out the value "waff"?(*yourAnimal).bark("waff")fmt.Println(*yourAnimal) // 2為什么這個(我擁有的最后一個Println //2)打印出值“waff”?它到底是什么意思(*動物)?這是取消引用動物,所以是一個值,該值被傳遞給接收者,接收者接受指向動物的指針而不是值?為什么這是合法的?正確的一個應該是地址嗎?
1 回答

森林海
TA貢獻2011條經驗 獲得超2個贊
接收器有一個指針接收器。這意味著,調用實際上是 ,它與上一個調用的對象相同。bark
(*yourAnimal).bark("waff")
(&(*yourAnimal)).bark("waff")
- 1 回答
- 0 關注
- 95 瀏覽
添加回答
舉報
0/150
提交
取消