The type system of the Go language

Source: Internet
Author: User
Tags email string

The go language allows user-defined types. When a user declares a type, the declaration provides a framework for the compiler to tell the necessary memory size and information.

declaring struct types

type user struct {    name  string    email string}

The above code declares a struct type with 2 fields, each based on a built-in type. Once the type is declared, you can use this type to create a value.

Declaring variables using struct types

var zuckjet user

We created a variable of type user named Zuckjet with the keyword var. When a variable is declared, the value corresponding to that variable is always initialized. This value is either initialized with the specified value or initialized with a value of 0. A value of 0 is 0 for a numeric value, and a value of 0 is an empty string for a string.
Let's look at how to declare a variable of type user and use a value other than 0 as the initial value.

zuckberg := user{  name: "zuckberg"  email: "zuckjet@gmail.com"}

Method

method to add a new behavior to a user-defined type. The method is actually a function, and when knowledge is declared, a parameter is added between the keyword Func and the method name.

func (u user) notify() {  fmt.Printf(u.name)}

In the preceding code, the arguments between the keyword Func and the function name are called receivers, tying the function to the type of receiver. If a function has a receiver, this function is called a method . Let's look at a simple demo:

package mainimport (    "fmt")type user struct {    name  string    email string}func (u user) notify() {    fmt.Println(u.name)}func main() {    bill := user{"zuckjet", "zuckjet@gmail.com"}    bill.notify()}// zuckjet

First we top a user type, and then notify uses the value receiver to implement a method, and finally we call the Notify method by creating a variable of type user.
When we call the method through Bill.notify (), the value of the U variable in the Notify method is equivalent to the variable bill. Why do I say the equivalent of a variable bill? Because
The method is declared using the value receiver, which is executed using a copy of the value.
Let's modify the above code a little bit to verify that:

package mainimport (    "fmt")type user struct {    name  string    email string}func (u user) notify() {    u.name = "zuckberg"    fmt.Println(u.name)}func main() {    bill := user{"zuckjet", "zuckjet@gmail.com"}    bill.notify()    fmt.Println(bill.name)}// zuckberg// zuckjet

Because the variable u is a copy of bill, changing the value of the property inside it does not affect the value of Bill itself.
It is worth noting that in the go language we can also use pointers to invoke methods that are declared by a value recipient, although this may seem unreasonable.
Let's take a look at the following code:

package mainimport (    "fmt")type user struct {    name  string    email string}func (u user) notify() {    fmt.Println(u.name)}func main() {    bill := &user{"zuckjet", "zuckjet@gmail.com"}    bill.notify()}// zuckjet

This time we define the variable bill as the pointer type, and when you call the Noify method with a pointer-type variable, you can still output the value of Bill's Name property. See here maybe you have some doubts, not that the method notify variable U is equivalent to the variable bill, why bill is a pointer type when you can also correctly output the Name property value. In fact, we can assume that go has done the following for us in the back:

(*bill).notify()

Above we are talking about using the value of the receiver declaration method, in addition we can also use the pointer recipient declaration method. Let's look at the following code:

package mainimport (    "fmt")type user struct {    name  string    email string}func (u *user) notify() {    fmt.Println(u.name)}func main() {    bill := user{"zuckjet", "zuckjet@gmail.com"}    bill.notify()}// zuckjet

Also, when calling the Notify method, go does something behind the back:

(&bill).notify()

When the above notify () method is transformed into:

  func (U *user) notify () {FMT. Println (u.name) fmt. Println (&u) fmt. Println (*u) fmt. Println (u)}//output Zuckjet0xc420088018{zuckjet Zuckjet@gmail.com}&{zuckjet zuckjet@gmail.com}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.