Golang is a rapidly growing programming language. It is as expressive and efficient as C language. It also has a large number of standard libraries and various tools to meet different programming needs. Golang also supports object-oriented programming, of which methods are an important feature. This article will introduce how to write methods yourself in Golang.
In Golang, a method is a function with a special signature defined on a structure (struct) or interface (interface). Methods can be value methods or pointer methods. Value methods operate on a copy of the structure, while pointer methods operate on the pointer of the structure, which means that the state of the structure can be modified in pointer methods.
The following is an example of usage:
package main import "fmt" type Rect struct { width, height float64 } func (r Rect) area() float64 { return r.width * r.height } func main() { r := Rect{3, 4} fmt.Println("Area:", r.area()) }
In the above code, we create a structure of type Rect. Then an area method is defined, the receiver is of type Rect, and the return value is the area of type float64. In the main function, we create a Rect instance and call the area method to calculate the area and print it.
In Golang, creating a method requires first defining a structure or interface. Then define methods on the structure or interface. The following is an example of defining usage methods and pointer methods:
type Person struct { name string age int } func (p Person) SayHello() { fmt.Println("Hello, my name is", p.name) } func (p *Person) SetAge(age int) { p.age = age }
In the above example, we define a Person structure that has two fields, name and age. We defined a SayHello method that takes no parameters and returns a value, it just prints out the person's name. We also define the SetAge method, which passes an integer as a parameter and sets it as the age property of the Person instance.
When we call the SayHello method, the Person instance is automatically passed to the method. In the SetAge method, we use a pointer as the receiver because we want to modify the internal state of the Person instance.
In Golang, calling methods requires the dot (.) operator. The following example shows how to call the method defined in the above example:
p1 := Person{"Tom", 20} p1.SayHello() // Hello, my name is Tom p2 := &Person{"Sam", 25} p2.SetAge(30) fmt.Println(p2.age) // 30
In this example, we create two Person instances, one using the structure literal and one using the new operator to return a pointer to the new allocation Pointer to the Person instance.
We use parentheses to call the p1.SayHello method. At this time we do not need to pass the instance because the method will automatically pass it as p1. For the p2.SetAge method, we need to pass the address pointing to the p2 pointer, otherwise the instance's properties cannot be accessed.
In Golang, methods can also be overloaded, that is, multiple methods are defined on the same receiver type. The following is an example of method overloading:
type Rect struct { width, height float64 } func (r Rect) Area() float64 { return r.width * r.height } func (r Rect) Perimeter() float64 { return 2 * (r.width + r.height) } func main() { r := Rect{3, 4} fmt.Println("Area:", r.Area()) fmt.Println("Perimeter:", r.Perimeter()) }
In the above example, we have defined two methods, Area and Perimeter, both of which operate on the Rect type, but their signatures are different. We can call these two methods respectively in the main function and calculate the area and perimeter of the rectangle.
The method is a very important feature in Golang and can provide us with convenience. How to write and call methods are basic skills that Golang programmers should master. We can use value methods and pointer methods to operate on structures, and overload methods to conveniently perform type operations. Of course, you also need to pay attention to the difference between pointer and value transfer, and use pointer methods to modify the properties of the structure when appropriate.
This article provides basic knowledge and skills on how to write methods in Golang. Readers should have a deeper understanding and mastery of the use of methods in Golang.
The above is the detailed content of Golang writes its own methods. For more information, please follow other related articles on the PHP Chinese website!