Pointer receiver (*T) — method operates on the original via pointer, can modify it, avoids copy overhead. Value receiver (T) — method operates on a copy, changes are discarded.
The choice affects whether the method has side effects. Pointer receivers are the only way to mutate a receiver's fields from within a method.
1type Balance struct {2 amount float643}45func (b *Balance) Deposit(amt float64) {6 b.amount += amt // modifies original7}89func (b Balance) String() string {10 return fmt.Sprintf("$%.2f", b.amount)11}1213b := Balance{100}14b.Deposit(50)15fmt.Println(b) // $150.00
When to use:
Common mistakes: