Pointer vs value receivers
A value receiver (func (s Session)) gets a copy of the struct. A pointer receiver (func (s *Session)) gets the address, so the method can mutate the original and avoid copying a large value.
Detailed Explanation
The receiver is the first argument. The difference between Session and *Session is the same difference as any other parameter: copy versus share. There is no second hidden rule, but there are conventions that keep a type's method set coherent.
Mutate? Use a pointer. func (s *Session) Refresh() writes s.Expires. With a value receiver those writes die with the copy, and the caller wonders why the session did not change. If any method on the type needs a pointer receiver, give the rest pointer receivers too, so callers do not have to remember which methods take a snapshot.
Copy is fine for small, immutable behaviour. func (s Session) Expired() bool only reads. A value receiver documents that, and a map or interface holding Session values can still call Expired. A pointer receiver on Expired would mean a Session value in an interface does not have Expired in its method set — only *Session would.
Method sets, not call syntax, are the trap. You can write s.Refresh() when s is an addressable Session and Refresh has a pointer receiver; the compiler inserts &s. You cannot call Refresh on a value that is not addressable: a map element, a function result, or a value stored in an interface. And when the value is in an interface, the method set is fixed at the moment of assignment: var h Handler = Session{} requires Session's method set to include every method of Handler. *Session methods do not count.
Consistency beats micro-optimizing copies. A 24-byte struct copied on a read-only method is cheaper than a heap allocation from taking its address. A 2 KiB struct should use pointer receivers. If you are unsure, pointer receivers for types you pass around as pointers (NewSession() *Session) and value receivers for tiny types (Point, Count).
What it costs. A value receiver copies; a pointer receiver can make the local escape to the heap. Neither is free in the abstract. Pick from mutation and method-set rules first, then look at a profile if the type is large and hot.
Code Examples
package main
import (
"fmt"
"time"
)
type Session struct {
Expires time.Time
}
func (s Session) Expired() bool {
return time.Now().After(s.Expires)
}
func (s *Session) Refresh() {
s.Expires = time.Now().Add(time.Hour)
}
func main() {
s := Session{}
s.Refresh() // compiler takes &s
fmt.Println(s.Expired())
}func mapTrap() {
m := map[string]Session{"a": {}}
// m["a"].Refresh() // compile error: cannot take address of m["a"]
s := m["a"]
s.Refresh()
m["a"] = s
}type Expirer interface {
Expired() bool
}
func sets() {
var e Expirer
e = Session{} // ok if Expired has a value receiver
e = &Session{} // always ok: *Session includes value-receiver methods
_ = e
}Common Gotchas
Pointer-receiver methods are missing from a value's method set
assigning `Session{}` to an interface that requires `Refresh()` fails if `Refresh` is on `*Session`.
Map values are not addressable
`m[k].Refresh()` does not compile for a pointer receiver. Copy out, mutate, write back, or store `*Session` in the map.
A value receiver cannot persist mutations
the method is not broken; it is editing a copy. Use a pointer if mutation is the point.
Mix receiver kinds on one type only when you have a reason
mixed sets confuse both humans and interface assignments.