pointers in Go: meaning and behaviour
Fast recall
Quick revision
Core answer
&value obtains an address and *pointer reads or changes the value at that address; a pointer's zero value is nil. Pointers let a function or method modify shared state and can avoid copying a large value. Go does not support pointer arithmetic, and taking an address is memory-safe because escape analysis keeps the value alive when required.Explain pointers in plain language, walk through one small Go example, and name the main boundary or limitation.
Listing syntax without explaining what the program stores or does.
Connect the definition to one realistic use and one important limitation.
Interview-ready guide
Interview answer
A pointer stores the address of another value.
&valueobtains an address and*pointerreads or changes the value at that address; a pointer's zero value isnil. Pointers let a function or method modify shared state and can avoid copying a large value. Go does not support pointer arithmetic, and taking an address is memory-safe because escape analysis keeps the value alive when required.For example: the function receives the address of
count, dereferences it, and changes the original value. Passingcountwithout&would give the function a copy instead. The code keeps the behaviour visible without unrelated setup.Use a pointer when mutation must be visible to the caller, when copying a large struct would be wasteful, or when
nilhas a deliberate meaning such as 'not supplied'. That choice keeps the data model and the code's intention aligned.The common mistake is dereferencing a nil pointer or using a pointer when ordinary value semantics would be simpler. A nil pointer has no value behind it, so
*porp.Fieldcan panic. Check optional pointers at boundaries, return useful errors with nil results, use value semantics by default for small data, and choose one receiver style consistently for a type.
pointers in Go: meaning and behaviour
Direct Answer
A pointer stores the address of another value. &value obtains an address and *pointer reads or changes the value at that address; a pointer's zero value is nil. Pointers let a function or method modify shared state and can avoid copying a large value. Go does not support pointer arithmetic, and taking an address is memory-safe because escape analysis keeps the value alive when required.
Deep Dive & Architecture
For a nil-pointer panic, start at the dereference in the stack trace and move backward to the assignment or function return that produced nil.
Check constructor results, map lookups that return pointer values, optional request fields, interface values that may hold typed nil pointers, and concurrent mutation.
Test a valid pointer, an explicit nil pointer, a missing lookup, and whether mutations should or should not be visible to the caller.
The common mistake is dereferencing a nil pointer or using a pointer when ordinary value semantics would be simpler. A nil pointer has no value behind it, so *p or p.Field can panic. Check optional pointers at boundaries, return useful errors with nil results, use value semantics by default for small data, and choose one receiver style consistently for a type.
Code Example
func increment(n *int) {
*n++
}
count := 2
increment(&count)
fmt.Println(count) // 3
Common Mistakes & Pitfalls
Listing syntax without explaining what the program stores or does.
Check your understanding
Answer these without looking back at the article.
- How do pointers compare with a value in Go?
- When should you use pointers in Go?
- What common mistakes should you avoid with pointers in Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.