Pointer receiver (*T) — receives a pointer to the value, can modify the original, avoids copying. Value receiver (T) — receives a copy, changes don't affect the original.
Pointer receivers are necessary when a method needs to mutate the receiver's state. They're also preferred for large structs to avoid the cost of copying.
1type Queue struct {2 items []string3}45// Pointer receiver — mutates the queue6func (q *Queue) Push(item string) {7 q.items = append(q.items, item)8}910func (q *Queue) Pop() (string, bool) {11 if len(q.items) == 0 {12 return "", false13 }14 item := q.items[0]15 q.items = q.items[1:]16 return item, true17}
Value receivers are appropriate for small, immutable types where copying is trivial and you want read-only semantics.
1type Point struct{ X, Y float64 }23func (p Point) DistanceTo(q Point) float64 {4 dx := p.X - q.X5 dy := p.Y - q.Y6 return math.Sqrt(dx*dx + dy*dy)7}
When to use:
Common mistakes: