This is a creation in Article, where the information may have evolved or changed.
0x01 Preface
The syntax of the go language is somewhat different, and the go approach seems obscure compared to other object-oriented languages.
Definition of the 0x02 method
In the Go language, the method and function are only one, and that means that the method func
has one more parameter than the identifier.
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)}
As we can see, the method is to func
notify
have a different user
type of parameter between and, which is u
called the u
receiver.
0X03 recipient
There are two types of receivers, one is the recipient of the value and one is the pointer receiver. As the name implies, the recipient of the value, is the type of the receiver is a value, is a copy, the method cannot make changes to its true receiver; the pointer receiver, the receiver's type is a pointer, is the recipient's reference, and the modification of this reference affects the true receiver. Define the method as above, and change it to be the user
*user
pointer receiver.
Recipients and objects
I believe there are many people who see this receiver is very distressed, in the end what this recipient is, what is the use of. When we learn a new language, we all pay attention to comprehend by analogy, and compare it with the language we already know. So we can figure out what the recipient is by comparing Go to other object-oriented languages with classes. Here we use php
to give examples.
In php
, we are going to define a method, first of all to define a class.
class User{ protected $email; protected $name; poublic 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; }}
Then instantiate an object and manipulate it, like this.
$user = new User('daryl', 'daryl@example');$user->changeEmail('daryl@example.com');$user->notify();
Next, we refer to the method definition of Go.
First, we are going to define a type first, such as user
good, and then we define the method.
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}
We have defined two methods, one is notify
that it is a value receiver method, and the other is changeEmail
that it is a pointer receiver method. You can see that the value receiver method, the recipient is a copy, cannot be modified, and the pointer receiver is a reference that can be modified.
Let's look at the call again.
daryl := {"daryl", "daryl@oldexample.com"}daryl.changeEmail("daryl@example.com")daryl.notify()
Look, is not very familiar! Yes, just like the code we just wrote php
, there's no! daryl
is the object, name
and email
is the attribute, notify
and changeEmail
is the method of it. Just, the difference is, we didn't put it class
in, but in another way to let them combine, have a relationship!
With respect to the value receiver and the pointer receiver, the GO has an implicit conversion at compile time, converting it to the correct recipient type. Just like this.
//daryl.changeEmail("daryl@example.com")(&daryl).changeEmail("daryl@example.com")wife := &daryl//wife.notify()(*wife).notify()
0x04 PostScript
The recent study of the Go language, it is undeniable that there are many people who evaluate the syntax of Go is ugly. However, its syntax is very simple, for people familiar with C, familiar with the object-oriented language of the class, a little contrast, you can find a lot of similarities.
The above is my own humble opinion, if there is wrong or wrong place, very welcome to point out! Life is always going to learn.