Value Receiver vs Pointer Receiver in Go

When to Use Which

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
}