Go Basic Learning-reciver

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Go is an object-oriented language, but he does not have the most object-oriented language keyword class, but this does not affect the excellent language to play object-oriented features.


The go language defines a class by using the struct keyword, which, when defined, simply defines the field and does not define a function, such as


Defines a person class and specifies the name and age attributes, but cannot specify the test method (behavior).

Type person struct {Name stringage uint8//func Test ()//Can not do this}

Go is through reciver to complete the function and class connection work, below, give the person class to add the test method

Package Mainimport ("FMT") func (per-person) Test1 () {per. Name = "Lniwn" per. Age = 23per.flag = 1}func (per *person) Test2 () {per. Name = "Lniwn" per. Age = 23per.flag = 2}type person struct {Name stringage  uint8flag byte//func Test ()//cannot do this}func main () {pp: = person{ }pp.test1 () fmt. Println (pp. Name, pp. Age, Pp.flag) Pp.test2 () fmt. Println (pp. Name, pp. Age, Pp.flag)}

Func keyword + (binding type) + function name (parameter) + return value

Binds a method to a type, the default is a copy (such as Test1), and can be used to keep the modified value in effect by passing pointers, such as test2.

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.