There are functions and methods in Go language. The essence of methods is functions, but methods and functions have different points.
function is a piece of code with independent functions, which can be called repeatedly to achieve code reuse.
method is the behavioral function of a class, which can only be invoked by objects of that class.
The method of Go language is a function that acts on a particular type of variable. This particular type of function is called Receiver (Receiver, Receiver, Receiver)
The concept of receiver is similar to that of driving this or self keywords in object-oriented languages.
Receivers of Go emphasize that methods have active objects, while functions have no active objects.
In Go, the type of receiver can be any type, not only the structure, but also any type other than the struct type.
As long as the recipient is different, the method name can be the same.
The grammatical format of the method:
func (receiver variable receiver type) method name (parameter list) (return value list){
// Method body
}
Receiver writes between func keyword and method name. Receiver can be struct type or non-struct type, and can be pointer type and non-pointer type.
When a variable in the receiver is named, the official recommendation is to use the first lowercase letter of the receiver type.
type employee struct { name,currency string salary float64 } func (e employee) printSalary() { fmt.Printf("Full name:%s , Salary:%s %.2f \n",e.name,e.currency,e.salary) } func testMethod01() { emp1 := employee{"Yoni","RMB",3000} emp1.printSalary() }
Use pointers as receivers
type rectangle struct { width,height float64 } func (r rectangle) setValue() { r.height = 10 r.width = 20 } func (r *rectangle) setValue2() { r.height = 20 r.width = 40 } func testMethod02(){ r1 :=rectangle{1,2} r2 := r1 fmt.Printf("Chang:%f,Gao:%f \n",r1.width,r1.height) r1.setValue() fmt.Printf("R1 After modification r1 Chang:%f,Gao:%f \n",r1.width,r1.height) fmt.Printf("R1 After modification r2 Chang:%f,Gao:%f \n",r2.width,r2.height) r1.setValue2() fmt.Printf("R1 After modification r1 Chang:%f,Gao:%f \n",r1.width,r1.height) fmt.Printf("R1 After modification r2 Chang:%f,Gao:%f \n",r2.width,r2.height) }
Write a body and method first.
type human struct { name, phone string age int8 } type student struct { human school string } type employee struct { human company string } func (h human) sayHi() { fmt.Printf("I am%s,Age%d,Contact information%s \n", h.name, h.age, h.phone) }
Inheritance of methods:
func testMethod11(){ s1 := student{human{"s1","138001",13},"First middle school"} e1 := employee{human{"e1","138002",30},"First enterprise"} s1.sayHi() //I am s1, age 13, contact number 138001 e1.sayHi() //I am e1, age 30, contact number 138002 }
Method rewriting:
func (s student) sayHi(){ fmt.Printf("I am%s,Age%d,I am here%s School, contact information%s \n", s.name, s.age, s.school,s.phone) } func testMethod12(){ s1 := student{human{"s1","138001",13},"First middle school"} s1.sayHi() //I am s1, age 13, I am in No. 1 Middle School, contact 138001 }