Home  >  Article  >  Backend Development  >  An in-depth discussion of some key concepts and usage of Go method pointers

An in-depth discussion of some key concepts and usage of Go method pointers

PHPz
PHPzOriginal
2023-04-25 10:42:18627browse

In the Go language, a method is a function bound to a certain type and can be called through an instance of that type. Go can define methods for any type, including custom types (structures, interfaces) and built-in types (strings, arrays, etc.), and even pointer types.

In Go, pointer (Pointer) is also a type. The pointer type can point to any type of variable, and the value of the variable can be modified through the pointer. Therefore, in Go, methods can also be defined on pointer types.

Below we will discuss in depth some key concepts and usage of Go method pointers.

Value types and pointer types

Let’s first look at an example and define a Person structure:

type Person struct {
    Name string
    Age  int
}

Now we can define an acceptPerson Type parameter method:

func (p Person) ChangeName(name string) {
    p.Name = name
}

Note that p here is a value of Person type, not a pointer. So when we call the above method, a new Person value is created and the modification does not affect the original variable.

person := Person{Name: "张三", Age: 20}
person.ChangeName("李四")
fmt.Println(person.Name) // 输出 "张三"

To avoid this situation, we can define a method that accepts the Person pointer type, so that when the Person variable is modified inside the method, a new one will not be created. Person variable, but directly modify the original Person variable.

func (p *Person) ChangeName(name string) {
    p.Name = name
}

person := &Person{Name: "张三", Age: 20}
person.ChangeName("李四")
fmt.Println(person.Name) // 输出 "李四"

You can see that when we define person as a pointer variable pointing to the Person type, and use the pointer type method ChangeName() When modifying the Name attribute, the Name attribute of the original Person variable will be modified.

The difference between pointer methods and value methods

In the Go language, in addition to the above-mentioned ability to modify variable values, there are several other differences between value methods and pointer methods.

First of all, pointer methods can modify the receiver (the type itself), while value methods cannot.

type Counter struct {
    count int
}

func (c *Counter) Add() {
    c.count++
}

func (c Counter) Get() int {
    return c.count
}

counter := Counter{count: 0}
counter.Add()
fmt.Println(counter.Get()) // 输出 1

In the above code, the Add() method is a pointer method, used to increase the value of the counter, and the Get() method is a value method, Used to get the value of the counter. As you can see, in the code, we first call the Add() method to increase the counter value, and then get the counter value through the Get() method.

Another difference is that when using a pointer type as the receiver, memory allocation when copying the type can be avoided, which can improve the performance of the program.

func (p *Person) ChangeAge(age int) {
    p.Age = age
}

person := &Person{Name: "张三", Age: 20}
person.ChangeAge(21)

In the above code, the ChangeAge() method is a pointer method used to modify the Age attribute of the Person variable. Since Person is a structure type, memory allocation occurs every time a variable of value type is passed. However, if the ChangeAge() method is defined as a pointer method, memory allocation can be avoided.

Call pointer type method from non-pointer type

In the Go language, if a method is defined on a pointer type, but we call the method using a non-pointer type, the compiler will automatically convert it to a pointer type call.

func (p *Person) ChangeName(name string) {
    p.Name = name
}

person := Person{Name: "张三", Age: 20}
person.ChangeName("李四")
fmt.Println(person.Name) // 输出 "李四"

In the above code, we define person as a variable of value type, but modify the value of the variable through the ChangeName() method. In this case, the compiler automatically converts person to a pointer type and calls the ChangeName() method.

Summary

In the Go language, the use of pointer types and methods is very common. By creating a new pointer type method, we can avoid memory allocation when copying the type. At the same time, pointer methods can also modify the receiver. Additionally, when calling a pointer type method using a non-pointer type, the compiler automatically converts the call to a pointer type. Therefore, when writing Go code, it is recommended to use pointer types and methods flexibly for better performance and program structure.

The above is the detailed content of An in-depth discussion of some key concepts and usage of Go method pointers. 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
Previous article:How to build Rtmp golangNext article:How to build Rtmp golang