Home  >  Article  >  Backend Development  >  How are methods defined and used in Go language?

How are methods defined and used in Go language?

PHPz
PHPzOriginal
2023-06-10 08:16:361650browse

Go language is a popular programming language in recent years. It is loved by developers because of its simplicity, efficiency, concurrency and other characteristics. Among them, method is also a very important concept in Go language. Next, this article will introduce in detail the definition and use of methods in the Go language.

1. Definition of method

A method in Go language is a function with a receiver (Receiver), which is a function bound to a certain type. The receiver can be a value type or a pointer type. Parameters for the receiver can be defined before the method name.

The definition format of the method is as follows:

func (接收器变量 接收器类型) 方法名(参数列表) (返回值列表) {
    // 方法体
}

Among them, the receiver variable is an identifier, the receiver type is a type name, and a pair of parentheses is used between the receiver type and the parameter list.

Let’s learn more about the definition of the method through an example:

package main

import "fmt"

type Person struct {
    name string
    age  int
}

// 带有接收器的方法
func (p Person) sayHello() {
    fmt.Printf("Hi, my name is %s. I am %d years old.
", p.name, p.age)
}

func main() {
    // 创建一个 Person 实例
    person := Person{"Tom", 18}
    // 调用方法
    person.sayHello()
}

In the above example, we defined a Person structure, which has two fields: name and age. , the method sayHello is defined on this structure. This method only accepts a receiver, which is p Person, indicating that the method is an instance method of the Person type.

The fields in the receiver can be directly called in the method, so the name and age fields of p can be directly accessed in the sayHello method.

2. Method usage

In Go language, the usage method is very simple. You only need to call it through instance.method name(). Here is a more complex example that shows method calls and the use of pointer type receivers:

package main

import "fmt"

type Point struct {
    x, y float64
}

// 普通实例方法
func (p Point) Distance(q Point) float64 {
    return ((p.x-q.x)*(p.x-q.x) + (p.y-q.y)*(p.y-q.y)) // 勾股定理求距离
}

// 指针类型接收器方法
func (p *Point) ScaleBy(factor float64) {
    p.x *= factor
    p.y *= factor
}

func main() {
    p1 := Point{1, 1}
    p2 := Point{4, 5}

    fmt.Printf("p1 到 p2 的距离为:%f
", p1.Distance(p2))

    p1.ScaleBy(2)
    fmt.Printf("p1 缩放后的坐标为:(%f, %f)
", p1.x, p1.y)
}

In the above example, we define a Point structure that has x and y two fields. We define two methods Distance and ScaleBy and bind them to the Point structure.

Distance is a common instance method that receives another Point type parameter and returns float64. This method is used to calculate the distance between two points.

In addition, we also define a pointer type receiver method ScaleBy, which is used to scale Point instances by a certain ratio. The receiver of this method is a pointer type, so all changes to p in ScaleBy are directly applied to the original instance.

Finally, in the main function, we created two Point instances p1 and p2 and called the Distance and ScaleBy methods. Since method calls in Go are not as cumbersome as in other object-oriented languages, the code is very concise.

Conclusion

Through the above introduction, I hope everyone has understood the definition and use of methods in the Go language. Different types of receivers can achieve different functions, making the methods in the Go language very flexible. The use of methods is not only very important in object-oriented programming, but also a core concept in functional programming. So mastering the use of Go language methods is very beneficial.

The above is the detailed content of How are methods defined and used in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn