What is a nil pointer, nil slice, and nil map in Go and how do they behave?
Understand nil pointers, nil slices and nil maps in Go: what panics, what is safe, and how to initialize maps with make to write values safely.
Expected Interview Answer
nil is the zero value for pointers, slices, maps, channels, functions and interfaces in Go. A nil pointer points to nothing (dereferencing it panics), a nil slice has no backing array (len and append work, indexing panics), and a nil map can be read but writing to it panics.
A nil slice behaves like an empty slice for most operations: len and cap are 0, ranging over it does nothing, and append allocates a new backing array — so you rarely need to pre-initialize it. A nil map is safe to read (returns the value type's zero value) but is not writable until you make() it, because it has no allocated hash table. A nil pointer holds no address, so any dereference triggers a runtime nil pointer dereference panic.
- nil slices append safely without pre-allocation
- Reading a nil map returns predictable zero values
- Clear, consistent zero values across reference types
- Enables optional/absent semantics via nil pointers
- Avoids unnecessary allocation for empty collections
AI Mentor Explanation
A nil pointer is a team sheet slot with no player named — call that player to bat and the innings crashes (a panic). A nil slice is an empty batting order: you can count it as zero and add batters (append) freely. A nil map is a locked scorebook — you may glance at blank pages and read nothing, but try to write a score and it refuses until the book is opened with make.
Step-by-Step Explanation
Step 1
Know the zero value
Pointers, slices, maps, channels, funcs and interfaces all default to nil when declared without initialization.
Step 2
Handle nil pointers
Never dereference a nil pointer; check for nil before using *p to avoid a runtime panic.
Step 3
Use nil slices freely
len, cap and range are safe on a nil slice, and append grows it — no need to pre-make an empty slice.
Step 4
Read but don't write nil maps
Reading a nil map returns the zero value; writing panics, so initialize with make(map...) before assigning.
Step 5
Guard exported APIs
Validate incoming pointers/maps for nil at boundaries, or document that callers must initialize them.
What Interviewer Expects
- Knowing nil is the zero value for reference types
- Explaining that dereferencing a nil pointer panics
- Understanding nil slices support len, range and append
- Knowing reads on a nil map succeed but writes panic
- Distinguishing nil slice from empty slice semantics
Common Mistakes
- Writing to a nil map instead of make-initializing it
- Assuming a nil slice must be initialized before append
- Dereferencing a pointer without a nil check
- Confusing a nil slice with a nil map's write behavior
- Thinking nil == empty in every context (e.g. JSON marshaling differs)
Best Answer (HR Friendly)
“In Go, nil is the default 'empty' value for things like pointers, slices and maps. A nil pointer points to nothing so using it crashes the program; a nil slice acts like an empty list you can safely add to; and a nil map can be read but must be set up with make before you store anything in it.”
Code Example
// nil pointer
var p *int
// fmt.Println(*p) // panic: nil pointer dereference
if p != nil { fmt.Println(*p) }
// nil slice: safe to len/range/append
var s []int
fmt.Println(len(s)) // 0
s = append(s, 1) // works, allocates backing array
// nil map: read ok, write panics
var m map[string]int
fmt.Println(m["x"]) // 0 (zero value), no panic
// m["x"] = 1 // panic: assignment to entry in nil map
m = make(map[string]int)
m["x"] = 1 // now safeFollow-up Questions
- What is the difference between a nil slice and an empty slice?
- Why can you read a nil map but not write to it?
- How does encoding/json treat a nil slice vs an empty slice?
- What happens when you range over a nil map or slice?
- How can a non-nil interface still compare unequal to nil?
MCQ Practice
1. What happens when you append to a nil slice?
append handles a nil slice as empty, allocating a new backing array and returning the grown slice.
2. What happens when you write to a nil map?
Writing to a nil map panics because it has no allocated hash table; you must make it first.
3. Reading a key from a nil map returns:
Reads on a nil map are safe and return the zero value of the map's value type.
Flash Cards
What happens if you dereference a nil pointer? — A runtime panic: nil pointer dereference.
Can you append to a nil slice? — Yes; len/cap are 0 and append allocates a backing array.
Can you read a nil map? — Yes, it returns the value type's zero value; writing panics.
What is the zero value of pointers, slices and maps? — nil for all of them.