A receiver is the value or pointer attached to a Go method. Choosing the wrong kind can make a mutation appear to succeed while leaving the original value unchanged. section 6.0 section 6.1
Choose the receiver according to the operation and the value being handled:
- Use a value receiver when the method should operate on a copy; mutations remain local and the original value is unchanged. section 6.1
- Use a pointer receiver when the method must mutate the original object; the method receives a copied pointer to that object. section 6.1
- A pointer receiver is required when appending through a slice receiver or when the receiver contains a value that cannot be copied. section 6.1
- A pointer receiver is generally preferable for large objects, but benchmark when size makes the choice uncertain. section 6.1
The mutation that disappears
With `func (c customer) add(v float64)`, a customer starting at 100.00 still prints 100.00 after `c.add(50.)`. Changing the declaration to `func (c *customer) add(v float64)` makes it print 150.00. A value receiver copies the customer; a pointer receiver updates the original.
Deferred methods evaluate receivers immediately
In `defer s.print()`, Go evaluates the receiver when the defer statement runs. A value receiver captures the current struct, so changing `s.id` from `foo` to `bar` still prints `foo`. A pointer receiver observes the later change and prints `bar`. section 6.6.2
type slice []int
func (s *slice) add(element int) {
*s = append(*s, element)
}
A slice receiver uses a pointer so that appending can replace the slice value held by the receiver. section 6.1


