Go basic types and zero values: meaning and behaviour
Fast recall
Quick revision
Core answer
0, booleans at false, strings at an empty string, and pointers, slices, maps, channels, functions, and interfaces at nil. Arrays and structs are zeroed recursively. Good Go types often make their zero value useful, but nil reference-like values do not all behave the same way.Explain zero values 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
Every variable in Go has a defined value even when no explicit initializer is written. Numbers start at
0, booleans atfalse, strings at an empty string, and pointers, slices, maps, channels, functions, and interfaces atnil. Arrays and structs are zeroed recursively. Good Go types often make their zero value useful, butnilreference-like values do not all behave the same way.For example: the declarations are immediately readable without manual initialization. A nil slice can be read, ranged over, and appended to, while writing to a nil map panics until
makeinitializes it. The code keeps the behaviour visible without unrelated setup.Rely on the zero value when it already represents a valid starting state, such as an empty counter, an unlocked
sync.Mutex, or a buffer with no data. That choice keeps the data model and the code's intention aligned.A common mistake is assuming every nil value behaves like an empty initialized value. Reading a missing key from a nil map is safe, but writing to that map panics; appending to a nil slice is safe. Know the zero value of each field, initialize maps and channels before use, and model optional business data with an explicit presence flag or pointer when zero is meaningful.
Go basic types and zero values: meaning and behaviour
Direct Answer
Every variable in Go has a defined value even when no explicit initializer is written. Numbers start at 0, booleans at false, strings at an empty string, and pointers, slices, maps, channels, functions, and interfaces at nil. Arrays and structs are zeroed recursively. Good Go types often make their zero value useful, but nil reference-like values do not all behave the same way.
Deep Dive & Architecture
For an unexpected zero or nil panic, identify whether the value was never assigned, deliberately reset, or missing from decoded input.
Then check the exact operation that requires initialization.
Check map writes, channel sends, pointer dereferences, interface values, and serialization differences between nil and empty collections. Test the untouched zero state, the first mutation, missing input, and any JSON or database boundary where nil and empty have different meaning.
A common mistake is assuming every nil value behaves like an empty initialized value. Reading a missing key from a nil map is safe, but writing to that map panics; appending to a nil slice is safe. Know the zero value of each field, initialize maps and channels before use, and model optional business data with an explicit presence flag or pointer when zero is meaningful.
Code Example
var count int // 0
var ready bool // false
var name string // ""
var items []string // nil, append works
var users map[string]int // nil, writes panic
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 zero values compare with explicit initialization or a constructor in Go?
- When should you use zero values in Go?
- What common mistakes should you avoid with zero values in Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.