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.