Use pointer receivers to modify state or avoid copying large structs, and value receivers for read-only operations on small types.
Use a pointer receiver when the method must modify the receiver's state or avoid copying large structs; use a value receiver for read-only operations on small types.
// Modify state: pointer receiver
func (c *Counter) Increment() {
c.count++
}
// Read-only: value receiver
func (c Counter) GetCount() int {
return c.count
}
Think of a value receiver as a photocopy of your data; changes to the copy don't affect the original. A pointer receiver is like handing over the original document, so any edits you make are permanent. Use pointers when you need to save changes or when the data is too big to copy efficiently.