When to use value types
1. If the recipient is a map,func or Chan, use value types (because they are reference types in themselves).
2. If the recipient is a slice, and the method does not perform the reslice operation and does not reallocate memory, use the value type.
3. If the recipient is a small array or a primitive value type struct type (such as time. Time type), and there are no modifiable fields and pointers, or the recipient is a simple primitive type like int and string, using a value type is fine .
A recipient of a value type can reduce a certain amount of garbage generation, and if a value is passed to the method of a value type recipient, a copy on a stack overrides allocating memory on the heap (but not guaranteed to be successful), so do not select the value type recipient for this reason until you understand what the code wants to do.
When to use pointer types
1. If the method needs to modify the recipient, the recipient must be a pointer type.
2. If the recipient is a containing sync. A Mutex or a struct similar to a synchronous field, the recipient must be a pointer, which avoids copying.
3. If the recipient is a large structure or an 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, and if you think that the parameters are a bit more, then it is large.
4. Can the recipient be modified when calling functions and methods concurrently in this method? A recipient of a value type creates a copy when the method is called, 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, either of them is a pointer type and may be modified, it is recommended to use the pointer type recipient, which increases the readability of the program
Remember to use the pointer recipient when you are still having doubts about what to use and who you are not.
- If the receiver is a map, the Func or Chan, and don ' t use a pointer to them. If the receiver is a slice and the method doesn ' t reslice or reallocate the slice, don't use a pointer to it.
- If the method needs to mutate the receiver, the receiver must is a pointer.
- If the receiver is a struct, that contains a sync. Mutex or similar synchronizing field, the receiver must is a pointer to avoid copying.
- If the receiver is a large struct or array, a pointer receiver was more efficient. How large is large? Assume it ' s equivalent to passing all its elements as arguments to the method. If that feels too large, it's also too large for the receiver.
- Can function or methods, either concurrently or when called from this method, be mutating the receiver? A value type creates a copy of the receiver when the method was invoked, so outside updates would not being applied to this rec Eiver. If changes must is visible in the original receiver, the receiver must is a pointer.
- If the receiver is a struct, an array or slice and any of its elements are a pointer to something so might be mutating, pref Er a pointer receiver, as it would make the intention more clear to the reader.
- If the receiver is a small an array or struct that's naturally a value type (for instance, something like the time. Time type), with no mutable fields and no pointers, or are just a simple basic type such as int or string, a value rece Iver makessense. A value Receiver can reduce the amount of garbage that can is generated; If a value is passed to a value method, an on-stack copy can be used instead of allocating on the heap. (the compiler tries to is smart about avoiding this allocation, but it can ' t always succeed.) Don ' t Choose a value receiver type for this reason without profiling first.
- Finally, when in doubt, use a pointer receiver.
Note: If the method recipient is the underlying type, use the value, because the underlying type cannot directly take the address, cannot self-convert to pointer mode, use the value of the recipient, the other can use the pointer to the recipient of the following error:
type int int
I: = &int (3)
Reference:
Go Code Review Comments
The choice of the recipient type of the Go language method
The choice of the go language receptive person