Home > Article > Backend Development > Focus on the syntax and usage of Golang methods
As a relatively young language, Golang also has its own unique implementation method. This article will focus on the syntax and usage of Golang methods.
1. Method definition
Methods can be defined for any type in Golang, including reference types and non-reference types. The method definition format is as follows:
func (t Type) methodName(parameterList) (returnList){ //方法体 }
Among them, t is the receiver, Type represents the receiver type, methodName is the method name, parameterList and returnList represent the method parameter and return value lists respectively.
The receiver can be a value type or a pointer type. The corresponding * or & symbol must be added when defining the method, for example:
func (p *Person) SetName(name string) { p.name = name }
2. Method call
In Golang Method calls are similar to function calls, except that a corresponding receiver needs to be provided when calling. For example:
package main import "fmt" type Person struct { name string } func (p *Person) SetName(name string) { p.name = name } func (p Person) GetName() string { return p.name } func main() { p := Person{name: "张三"} fmt.Println(p.GetName()) // 输出:张三 p.SetName("李四") fmt.Println(p.GetName()) // 输出:李四 }
In the above example, a structure called Person is first defined, which contains a name attribute of string type. Then, two methods are defined: SetName and GetName, which are used to set and get the value of the name attribute respectively. In the main function, a variable p of type Person is first created, and its GetName method is called, and the name attribute value of p is "Zhang San". Then, the SetName method was called to modify the value to "John Doe", and then the GetName method was called to output the modified name attribute value "John Doe".
3. Value and pointer receivers
As can be seen from the previous code examples, methods can be defined for value types or pointer types. So what is the difference between these two methods?
Note: Different receiver types cannot be assigned to each other.
1. Value receiver
When the method is defined, if the receiver is a value type, then the receiver will be copied when the method is called. Therefore, operations performed on the copied instance have no impact on the original instance. For example:
package main import "fmt" type Person struct { name string } func (p Person) GetName() string { return p.name } func (p Person) SetName(name string) { p.name = name } func main() { p1 := Person{name: "张三"} p2 := p1 p2.SetName("李四") fmt.Println(p1.GetName()) // 输出:张三 fmt.Println(p2.GetName()) // 输出:李四 }
The method Setname defined by the value type receiver will copy the original value when instantiated, so p1 and p2 are actually two different instances. So when p2 calls the SetName method to modify the attribute value, it has no effect on p1.
2. Pointer receiver
When the method is defined, if the receiver is a pointer type, then when the method is called, the object pointed to by the pointer is actually operated. If the method modifies the object, it will directly affect the original object. For example:
package main import "fmt" type Person struct { name string } func (p *Person) GetName() string { return p.name } func (p *Person) SetName(name string) { p.name = name } func main() { p1 := &Person{name: "张三"} p2 := p1 p2.SetName("李四") fmt.Println(p1.GetName()) // 输出:李四 fmt.Println(p2.GetName()) // 输出:李四 }
The method SetName defined by the pointer type receiver will directly modify the attribute value of the pointed object, and p1 and p2 point to the same object, so when one of them calls the SetName method to modify the attribute value, It also has an impact on another object.
4. Structure embedding method
Golang allows structure embedding, that is, a structure can contain member variables of other structure types. This method is often called combination.
When embedding a structure, you can add * or & before the type name to indicate embedded pointer type or value type. For example:
type Person struct { name string } func (p *Person) GetName() string { return p.name } type Employee struct { *Person age int } func main() { emp := &Employee{Person: &Person{"张三"}, age: 28} fmt.Println(emp.GetName()) // 输出:张三 }
In this example, a Person structure type is first defined, including a name attribute of string type and a GetName method. Then, an Employee structure type is defined, the Persion structure type is embedded, and an age attribute of integer type is added. When finally instantiating emp, use the curly brace initialization method to initialize an object of this type for the Persion property. When calling the GetName method of emp, it actually calls the GetName method of the Persion property inside emp, thus outputting "Zhang San".
5. Summary
Methods in Golang are similar to function methods, but have a clearer function scope. The method defined by the pointer type receiver can directly modify the attribute value of the object, thereby increasing the flexibility of the method and avoiding the trouble of reassigning the value through the return value. When using structure embedding, redundant code can be avoided, making the program more concise.
The above is the detailed content of Focus on the syntax and usage of Golang methods. For more information, please follow other related articles on the PHP Chinese website!