Pointer vs value
A value copy duplicates the bits. A pointer shares one object. Choose a pointer when you need mutation or when the struct is large enough that copying shows up in a profile. Choose a value when the type is small and you want independent copies.
Detailed Explanation
s := other copies a Session. p := &other copies an address. After the copy, s.Token = "x" does not change other. p.Token = "x" does. That is the semantics. The memory question is where those bits live and how many times the CPU moves them.
Small values. int, time.Time (24 bytes on 64-bit), a Point with two ints — pass by value. The copy is registers or a few loads. A pointer would add a heap alloc if the local escaped, plus a cache miss to follow it.
Large values. A struct with a [1024]byte or many fields should be passed as *T if you pass it often. Measure: testing.B with b.N calls. There is no official "too big" number; a common rule of thumb is "a few dozen bytes," not a law.
Slices, maps, channels are already headers. Passing a slice copies the header, not the array. Passing *[]T is unusual. Passing *map is also unusual; the map header already points at shared buckets. Pointers to these types show up when you must reassign the caller's header (append in a method: pointer receiver on the struct that contains the slice).
Interfaces hold a copy of the value or a pointer depending on what you assign. Assigning a large struct to an interface copies it into the box (and it escapes). Assign *T if the interface is just a handle.
What it costs. Wrong value copies burn CPU. Wrong pointers burn heap and confuse mutation. Method receivers should match the rest of the type. Consistency is part of the cost model: a type you always handle as *T should have pointer receivers.
Code Examples
package main
type Point struct{ X, Y int }
func moveVal(p Point) { p.X++ } // caller unchanged
func movePtr(p *Point) { p.X++ }
func main() {
p := Point{1, 2}
moveVal(p)
movePtr(&p)
}type Buffer struct{ buf []byte }
func (b *Buffer) Append(p []byte) {
b.buf = append(b.buf, p...) // must update the struct's header
}Common Gotchas
A value receiver plus mutation is a silent no-op
the copy changes, the caller does not.
Putting a large struct in an `any` boxes a copy
use a pointer.
Slice headers share the array even when passed by value
Slice headers share the array even when passed by value
Escape analysis may heap-allocate because you took a pointer, not because the st
Escape analysis may heap-allocate because you took a pointer, not because the struct is large