We all know in C# or Java that a Class must contain member variables and methods, and the same is true for Struct in GO language. We can also define a series of methods for Struct.
1. How to define a method?
Go's method is to add a receiver before the function, so that the compiler knows which type of this method belongs to. For example:
package demo1 import ( "fmt" ) type Student struct { Name string Age int Class string } func (stu Student) GetUserInfo(student Student) { ("Student name: %v Age: %v Class: %v ",,,) }
The above code defines a Student's structure, and then creates three methods for this structure. We can access the methods in this structure through the [Instance Name. Method Name].
package main import "Function/demo1" func main(){ student:={ Name:"XiaoMing", Age:20, Class:"3-2", } (student) }
2. Recipient type problem
The receiver of GetUserInfo above is a Student type, and a problem will occur here. If I set the class operation, the value of the corresponding instance will not be changed, it is just a copy. The following example will illustrate this problem. The recipient of the Set method below is Student
type Student struct { Name string Age int Class string } func (stu Student) SetStudentName(name string) { = name }
This class is used in main function
func main(){ student:={ Name:"XiaoMing", Age:20, Class:"3-2", } ("LiLie") ("Name: %v", ) }
result:
Name: XiaoMing
As you can see, the Name has not been modified here.
If we use the Student pointer type as the receiver, this value will be modified. Examples are as follows:
func (stu *Student) SetStudentName(name string) { = name //Why can stu directly come out Name here? It is the syntax sugar of Go equivalent to (*stu).Name}
The results of the operation again are:
Name: LiLie
3. When to use the value type receiver, and when to use the pointer type receiver.
Rough conclusion: If you don't know how to choose, use pointers. But sometimes, it is more reasonable to use value recipients, especially for efficiency considerations, such as: small structs that do not require modification and basic data types. Here are some useful guidelines:
- - If the receiver is map, func, or chan, no pointer is required. If it is a slice, and the method will not reslice or allocate slice from, do not use pointers;
- - If the method needs to modify the receiver, a pointer must be used; - If the receiver is a struct containing or similar synchronization fields, the receiver must use a pointer to avoid copying;
- - If the receiver is a large structure or array, using pointers will be more efficient. But how big is it? If all elements (fields or array elements of struct) are passed as parameters of the method are considered too large, then they are also too large as the receiver. (A little rough, all elements have more memory than the pointer size, you can consider using pointers);
- - If the receiver is a structure, array or slice, and their elements are pointers, and the data pointed to be changed, it will be more reasonable to use pointers to the receiver; (A little bit surrounded, then the general principle: using pointers is correct);
- - If the receiver is a small array, or a small structure without variable fields or pointers, or the structure field is just a simple basic type, the value receiver will be more reasonable; the value receiver can reduce the pressure of garbage collection and will generally be allocated on the stack first (remember that it is general, because it is possible to escape); but unless there is performance testing verification, don't select the value receiver because it can introduce the pressure of garbage collection;
Finally, I would like to emphasize that if you are not sure about it, use the pointer to receive it.
This is all about this article about functions and methods in the go language. I hope it will be helpful to everyone's learning and I hope everyone will support me more.