Home > Article > Backend Development > Getting started with golang methods
Golang is a strongly typed, statically typed programming language that has excellent performance, concurrency, simplicity and security. Among them, method is a very important concept in Golang, which allows programmers to operate structures more conveniently. This article will introduce readers to methods in Golang, including method definition, calling and inheritance, to help beginners quickly become familiar with the use of methods.
1. Definition of method
A method is a special function that must have a receiver before it can be called. The receiver can be a structure type, or a non-structural type (such as int, float, etc.). Methods can be defined in any package, not just the package where the struct is located.
The definition format of the method is as follows:
func (接收器变量 接收器类型) 方法名(参数列表)(返回值列表){ // 函数体 }
The receiver variable can be any legal variable name, and the receiver type can be any type, but it must be a type in the current package. Inside a method, you can use receiver variables to access the fields and methods contained by the receiver.
There are two types of receivers:
The sample code is as follows:
type Rect struct{ width, height float64 } // 定义求面积的方法(值类型接收器) func (r Rect) area() float64{ return r.width * r.height } // 定义求面积的方法(指针类型接收器) func (r *Rect) areaPtr() float64{ return r.width * r.height }
In the above code, we define two methods for finding the area of a rectangle, one uses a value type receiver and the other uses a pointer type receiver. .
2. Method invocation
is similar to function invocation. Method invocation requires the use of the dot operator. When calling a method, Golang automatically resolves the method's receiver type and passes the receiver to which the method belongs as the first parameter.
The sample code is as follows:
package main import "fmt" type Rect struct{ width, height float64 } // 定义求面积的方法(值类型接收器) func (r Rect) area() float64{ return r.width * r.height } // 定义求面积的方法(指针类型接收器) func (r *Rect) areaPtr() float64{ return r.width * r.height } func main(){ rect := Rect{10, 20} // 调用值类型接收器的方法 fmt.Println(rect.area()) // 输出:200 // 调用指针类型接收器的方法 fmt.Println(rect.areaPtr()) // 输出:200 // 使用&符号获取指针,再调用指针类型接收器的方法 fmt.Println((&rect).areaPtr()) // 输出:200 }
In the above code, we created a rectangle rect and called its two methods.
It should be noted that when using the method of a value type receiver, if the receiver is a value type of a structure, its field values will not be modified. When using the pointer type receiver method, if the receiver is a pointer type of a structure, its field values will be modified.
3. Inheritance of methods
In Golang, a type can achieve inheritance by combining other types. During the inheritance process, methods in the subtype can override methods in the parent type to implement its own logic.
The sample code is as follows:
package main import "fmt" type Shape interface{ area() float64 } type Rect struct{ width, height float64 } // 定义矩形类的方法 func (r Rect) area() float64{ return r.width * r.height } type Circle struct{ radius float64 } // 定义圆形类的方法 func (c Circle) area() float64{ return 3.14 * c.radius * c.radius } func main(){ rect := Rect{10, 20} circle := Circle{10} // 创建形状对象,使用接口类型接收 var shape Shape // 将矩形类型赋值给接口类型 shape = rect fmt.Println(shape.area()) // 输出:200 // 将圆形类型赋值给接口类型 shape = circle fmt.Println(shape.area()) // 输出:314 }
In the above code, we define a Shape interface, including a method to calculate the area. Next, we defined two shape types, Rect and Circle, which respectively cover the area method in the Shape interface. Finally, we calculate the area by assigning the Rect and Circle types to the Shape interface type respectively.
Summary:
This article introduces knowledge about the definition, calling and inheritance of methods in Golang. By studying this article, readers can understand the role of the important concept of method in Golang and master its basic usage skills. Based on this, readers can further learn Golang's syntax and applications, master more advanced method usage skills, and improve their programming abilities.
The above is the detailed content of Getting started with golang methods. For more information, please follow other related articles on the PHP Chinese website!