0x01 前言
Go語言的語法實在有些不一樣,與其他面嚮對象語言相比,Go的方法似乎有些晦澀。
0x02 方法的定義
在Go語言裏,方法和函數只差了一個,那就是方法在func和標識符之間多了一個參數。
type user struct {
name string,
email string,
}
//這裏是函數的定義
func notify(email string) {
fmt.Println("Email is %s", email)
}
//這裏是方法的定義
func (u user) notify(email string) {
fmt.Println("Email is %d", email)
}
我們可以看到,方法是在func和notify之間多了一個user類型的參數u,這個u就稱作接收者。
0x03 接收者
接收者有兩種,一種是值接收者,一種是指針接收者。顧名思義,值接收者,是接收者的類型是一個值,是一個副本,方法內部無法對其真正的接收者做更改;指針接收者,接收者的類型是一個指針,是接收者的引用,對這個引用的修改之間影響真正的接收者。像上面一樣定義方法,將user改成*user就是指針接收者。
接收者與對象
相信很多人看到這個接收者之後都很苦惱,到底這個接收者是什麼,是幹什麼用的。我們在學習一門新的語言的時候,都講究觸類旁通,和我們已經瞭解的語言作對比。那麼我們就通過拿 Go 和其它帶有類的面向對象的語言做對比來搞清楚接收者是什麼。這裏我們用 php
來舉例子。
在php中,我們要定一個方法,首先是要定一個類。
class User
{
protected $email;
protected $name;
public function __construct($name, $email)
{
$this->email = $email;
$this->name = $name;
}
public function notify()
{
echo "Email is {$email}.\n";
}
public function changeEmail($email)
{
$this->email = $email;
}
}
然後再實例化一個對象,進行操作,像這樣。
$user = new User('dary1', 'dary1@example');
$user->changeEmail('[email protected]');
$user->notify();
接下來,我們參照着來寫一下Go的方法定義。
首先,我們是先要定義一個類型,比如就是user好了,然後再定義方法。
type user struct {
name string
email string
}
func (u user) notify() {
fmt.Println("Email is %d", u.email)
}
func (u *user) changeEmail(email string) {
u.email = email
}
我們定義了兩個方法,一個是notify,它是值接收者方法;還有一個是changeEmail,它是指針接收者方法。可以看到,值接收者方法,接收者是一個副本,無法修改;指針接收者是引用,可以修改。
我們再來看一下調用:
dary1 := {"dary1", "[email protected]"}
dary1.changeEmail("[email protected]")
dary1.notify()
看看,是不是很熟悉!對,就像我們剛剛寫過的php代碼一樣,有沒有!dary1就是對象,name和email就是屬性,notify和changeEmail就是它的方法。只是,不同的是,我們沒有將它放到class中,而是用另外一種方式讓它們結合了,有了關係!
關於值接收者和指針接收者,其實Go在變異的時候有一個隱式轉換,將其轉換爲正確的接收者類型。就像下面這樣。
//daryl.changeEmail("[email protected]")
(&daryl).changeEmail("[email protected]")
wife := &daryl
//wife.notify()
(*wife).notify()