Go: Pointer vs. value receiver
- For a given type, don't mix value and pointer receivers.
- If in doubt, use pointer receivers (they are safe and extendable).
You must use pointer receivers
- if any method needs to mutate the receiver,
- for structs that contain a
sync.Mutexor similar synchronizing field (they musn't be copied).
You probably want to use pointer receivers
- for large structs or arrays (it can be more efficient),
- in all other cases.
You probably want to use value receivers
- for simple basic types such as
- for small arrays or structs that are value types, with no mutable fields and no pointers.
You may want to use value receivers
- for slices with methods that do not reslice or reallocate the slice.
Be the first to comment!