Overview
A lot of people (especially beginners) often ask a question when writing the go code, is whether the recipient type of a method should be a value type or a pointer type, which is well explained on the go wiki, and I'll translate it.
When to use value types
1. If the recipient is a map,func or Chan, the value type is used (because they are reference types themselves).
2. If the recipient is a slice and the method does not perform a reslice operation, and does not reassign the memory to the slice, the value type is used.
3. If the recipient is a small array or a native value type struct type (for example, time). Time type), and there are no modifiable fields and pointers, or if the recipient is a simple base type like int and string, use value types.
A recipient of a value type can reduce a certain amount of garbage generation, if a value is passed into a method of a value type recipient, a copy on the stack replaces allocating memory on the heap (but not guaranteed to succeed), so do not select the value type recipient for this reason until you understand what the code is trying to do.
When to use a pointer type
1. If the method needs to modify the recipient, the recipient must be a pointer type.
2. If the recipient is a included sync. A Mutex, or a structure similar to a synchronization field, that the recipient must be a pointer to avoid copying.
3. If the recipient is a large structure or array, the pointer type recipient is more efficient. (How big is it?) Suppose you pass all the elements of the recipient as arguments to the method, and if you think the argument is a bit more, then it's big.
4. Can the recipient be modified when calling functions and methods concurrently in this method? The recipient of a value type creates a copy of the method call, so the external modification does not work on the recipient. If the modification must be visible to the original recipient, then the recipient must be a pointer type.
5. If the recipient is a struct, array or slice, any one of them is a pointer type and may be modified, it is recommended that you use the pointer type recipient, which will increase the readability of the program
When you have a doubt about this, or you don't know which recipient to use, remember to use the pointer recipient.
About the recipient's name
The community-appointed recipient name is a type or two-letter abbreviation (like C or CL for the Client). Don't use generic names like me,this or self, and don't use overly descriptive names, and finally, if you use C in one place, don't use CL anywhere else.