Pointer receiver (*T) — method gets a pointer to the value, can modify the original, avoids copying. Value receiver (T) — method gets a copy, changes don't affect the original.
The receiver type determines whether the method has side effects. Pointer receivers are the only way to mutate a receiver's state from within a method.
1type Counter struct {2 count int3}45// Pointer receiver — can modify6func (c *Counter) Increment() {7 c.count++ // modifies original8}910// Value receiver — read-only11func (c Counter) Value() int {12 return c.count13}1415var c Counter16c.Increment()17c.Increment()18fmt.Println(c.Value()) // 2
Performance consideration: Value receivers copy the entire struct. For small structs this is negligible. For large structs, pointer receivers avoid the copy.
When to use:
Common mistakes: