Pointers
A pointer is like a house address on a sticker.
It does not hold the value itself.
It tells you where the labeled box lives.
Go has no pointer math. That keeps pointers safer than in C.
Address with &
&x takes the address of x.
That is like writing down the house number.
You store that address in a pointer variable.
Looking inside with *
*p reads or writes the value at that address.
That is like opening the door and looking inside the box.
If you change *p, the original x changes too.
Pointers in functions
Pass a pointer so the function can change the original value.
Without a pointer, Go passes a copy.
Changes to a copy stay inside the function.
new
new(T) returns a pointer to a starter value of type T.
The box exists. It starts with the zero value.
nil pointers
A pointer's empty value is nil.
nil means the sticker points nowhere.
Always check before you follow a pointer if nil is possible.
Start with normal values. Use pointers when you need to share or change the original on purpose.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.