Pointers
A pointer holds the address of a value. *T is "pointer to T." &x takes the address of x. *p reads or writes the value p points at. The zero pointer is nil.
Detailed Explanation
Go copies values. s := other copies a struct. If two functions must see the same Session and update its expiry, they need the same address: *Session. Pointers are how you opt into sharing. They are not how you iterate strings, and they do not support arithmetic.
The type is visible. A parameter of type Session is a copy. A parameter of type *Session is an address. There is no hidden reference type for structs, the way Java objects are always references. Slices, maps, and channels are headers that already contain a pointer to shared data; taking their address is a different, less common need.
Dereferencing is often implicit on structs. p.Expires means (*p).Expires when p is *Session. Method calls do the same. You still write *n for a *int when you want the integer.
nil. A pointer you declared and did not point at anything is nil. Reading or writing *p panics. Passing nil to a function that immediately does p.Field panics. Document whether a *T parameter may be nil. Inside the function, check before you dereference.
You can take the address of a composite literal. p := &Session{Token: "abc"} allocates a Session and yields *Session. You can also take the address of a local variable; if that pointer escapes the function, the variable lives on the heap. You cannot take the address of a constant or of a map element's value (&m[k] is illegal).
No pointer arithmetic. p + 1 does not compile. unsafe.Pointer and uintptr exist for FFI and for the runtime; they are not part of ordinary application code. Walk a slice with an index or range.
What it costs. A pointer is a word. Following it can miss the cache if the value is far away. A small struct passed by value is often faster than a pointer, because the copy stays in registers and does not allocate. Escape analysis decides heap vs stack; taking a pointer is the most common reason a local escapes. Use a pointer when you need sharing, mutation, or when the struct is large enough that copying shows up in a profile.
Code Examples
package main
import "fmt"
type Session struct {
Token string
}
func touch(s *Session) {
s.Token = "new" // implicit dereference
}
func main() {
s := Session{Token: "old"}
touch(&s)
fmt.Println(s.Token) // new
p := &Session{Token: "abc"}
fmt.Println(p.Token)
}func incr(n *int) {
if n == nil {
return
}
*n++
}func illegal() {
m := map[string]int{"a": 1}
// p := &m["a"] // compile error: cannot take address of map element
_ = m
}Common Gotchas
Dereferencing `nil` panics
check pointers you did not create, especially from other packages.
`&m[k]` does not compile
copy the value out, or store pointers in the map (`map[string]*T`).
Pointer identity is not value equality
two `*T` compare equal when they are the same address, or both nil. Compare `*p == *q` when you mean the values, after nil checks.
Methods with pointer receivers require an addressable value or a pointer
a map value or a function result is not addressable, so `m[k].Mutate()` fails if `Mutate` has a pointer receiver.