Pointer types
Pointers model indirection and shared access, but addressability and method-set rules matter as much as nil checks.
Canonical guidance
*Tis a distinct type fromT- not every expression is addressable
- pointer receiver calls may work through addressable values, but interface method sets still matter
Use when
- reasoning about mutation through references
- checking whether
&xor*pis legal - explaining nil pointer panics or method call legality
Avoid
- using pointers for small immutable values without semantic benefit
- assuming maps or interface values are addressable
- conflating pointer convenience with interface satisfaction
Preferred pattern
type Counter struct {
n int
}
func (c *Counter) Inc() { c.n++ }
Anti-pattern
- taking the address of a map element
Explanation: This is tempting because the element looks like an lvalue, but map indexing does not produce an addressable value.
Why
- pointer rules interact with method calls, interfaces, and panics in ways agents need to retrieve precisely