Overview
Many people (especially newbies) often ask a question when writing Go language code, that is, should the recipient type of a method be a value type or a pointer type? Go's wiki has given a good explanation of this, so I'll translate it.
When to use value types
1. If the recipient is a map, func, or chan, use value types (because they are reference types themselves).
2. If the recipient is a slice and the method does not perform the reslice operation and does not reassign memory to the slice, use the value type.
3. If the recipient is a small array or native value type structure type (such as type), and there are no fields and pointers that can be modified, or the recipient is a simple basic type like int and string, just use the value type.
A value type recipient can reduce the generation of garbage. If a value is passed into a value type recipient's method, a copy on the stack will replace allocating memory on the heap (but not guaranteed to be successful). So don't choose a value type recipient for this reason before you figure out what the code wants to do.
When to use pointer type
1. If the method needs to modify the recipient, the recipient must be of the pointer type.
2. If the recipient is a structure containing or similar synchronous fields, the recipient must be a pointer to avoid copying.
3. If the recipient is a large structure or array, then the pointer type recipient is more efficient. (How big is it? Suppose you pass all the elements of the recipient as parameters to the method. If you think there are a bit more parameters, then it is big).
4. Can the recipient be modified when calling functions and methods concurrently from this method? A value type recipient creates a copy when the method is called, so external modifications cannot be applied to the recipient. If the modification must be visible to the original recipient, the recipient must be of the pointer type.
5. If the recipient is a structure, array or slice, any of which elements are pointer types and may be modified, it is recommended to use pointer type recipients, which will increase the readability of the program.
When you still have doubts after reading this or don't know which recipient to use, then remember to use pointer recipient.
About the naming of the recipient
The recipient naming of the community convention is an abbreviation of one or two letters of the type (like c or cl for Client). Don't use general names like me, this or self, and don't use overdescribing names. Finally, if you use c in one place, then don't use cl in other places.