methods

The GO language has functions and methods, and methods are functions by nature. But methods and functions are a little bit different

2.1 Differences between methods and functions in GO

A method is the behavior of a class or object. A function is a block of code with its own function

Methods have receivers, functions do not

Functions can’t have the same name but methods can

Package main import "FMT" type employee struct {name,currency string salary float64 The employee) {FMT. Printf (" % s, % s, % 2 f \ n ", e.n ame, e.c. with our fabrication: urrency, e.s alary)} / / this method func employee (e) printSalary () { fmt.Printf("%s,%s,%.2f\n",e.name,e.currency,e.salary) } func main() { emp:=employee{"JackMa","$",20000000} Emp. PrintSalary () printSalary(emp)} /*Copy the code

There are two main reasons why a program can be written in functions but still use methods.

  1. Go is not a pure object-oriented language; it does not support classes. So methods are implementing behavior similar to classes.
  2. Methods with the same name can be defined on different types, while functions with the same name are not allowed. For example, the area of a circle is different from that of a rectangle
package main import "fmt" type Rectangle struct { width,height float64 } type circle struct { radius float64 } func (r Rectangle)area() float64 {return r.width* r.eight} func (c circle)area() float64 {return c.adius * c.adius *3.14159265 } func main() {r1:=Rectangle{10,4} c1:=circle{10} FMT.Println(" Rectangle area :",r1. Area ()) FMT.Println(" Rectangle area :",r1. ",c1.area())} /* Output result: rectangle area: 40 circle area: 314.159265 */Copy the code

If the recipient of a method is not a pointer, it is actually getting a copy of the method without actually changing the original data in the recipient. When the pointer is the recipient, the original value is changed.

package main import "fmt" type rectangle struct { width,height float64 } func (r rectangle)setValue() { Printf(" rectangle r :%p\n",&r) // 0xC00000A0f0 r.Eight =10} func (r * Rectangle)setValue2() { Printf("setValue method :%p\n",r) //0xc00000a0b0 r.eight =20} func main() {r1:=rectangle{5,8} r2:=r1 FMT.Printf("r1 address: %p\n",& R1) // 0xC00000A0b0 FMT.Printf(" R2 address: %p\n",& R1) %p\n",&r2) //0xc00000a0c0 r1.setValue() fmt.Println("r1.height=",r1.height) //8 fmt.Println("r2.height=",r2.height) //8 fmt.Printf("-------------------\n") r1.setValue2() fmt.Printf("*******************\n") FMT. Printf (" r1. Height = % f \ n ", r1. Height) / / r1, height = 20.000000 FMT. Printf (" r2. Height = % f \ n ", r2, height) / / r2. The height = 8.000000 }Copy the code

2.2 Method Inheritance

Methods are inheritable, and if an anonymous field implements a method, the struct containing that anonymous field can also call the method in that anonymous field.

Package main import "FMT" type Human struct {name,phone string age int} type student struct {Human // Func (h *Human) sayHi() {FMT.Printf() {FMT. My contact information is: %s\n",h.name,h.age,h.phone) } func main() { s1:=student{Human{"JackMa","1388888888",13},"Mit"} E1 :=Employee{Human{"PonyMa","1399999999",123},"shenzhen"} s1. SayHi () e1. SayHi ()} /* Hello everyone, I'm PonyMa. I'm 123 years old. My contact information is 1399999999 */Copy the code

2.3 Method Rewriting

package main import "fmt" type Person struct { name,phone string age int } type Student struct { Person school string } Type teacher struct {Person object string} func (h *Person)sayHello() {FMT.Printf(" Func (s *Student) sayHello() {FMT.Printf(" hello, I am %s, I am %s, Func (t *Teachers) sayHello() {FMT.Printf(" hello, I am %s, I am %s, I am % S, I am %s\n",s.name,s.age, s.hone)} func (t *Teachers) sayHello() {FMT.Printf(" hello, I am %s, I am %s, %s\n",t.name,t.age, t.hone)} func main() {s1:=Student{Person{" Person ","13885555320",13}," Person "} SayHello () t1:= teacher {Person{" 12345679810",28}," sayHello "} s1. SayHello () t1. SayHello ()}Copy the code