Common zero values mistakes
Fast recall
Quick revision
Core answer
Name a concrete zero values mistake, show its visible effect, and explain how to prevent and test it.
Saying only “be careful” without tracing the incorrect behaviour.
Show the smallest failing example and the regression test that protects the rule.
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.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. For example:
var counts map[string]int; counts["go"]++panics because the map has not been created.counts := make(map[string]int)establishes writable storage.A nil slice and an empty non-nil slice both have length zero but may encode differently in JSON. Also, zero can be a real business value, so it cannot always mean 'not provided'. This is why the surrounding data contract matters as much as the syntax.
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. Test the untouched zero state, the first mutation, missing input, and any JSON or database boundary where nil and empty have different meaning.
Common zero values mistakes
Direct Answer
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.
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
counts := make(map[string]int)
counts["go"]++
var items []string
items = append(items, "first")
Common Mistakes & Pitfalls
Saying only “be careful” without tracing the incorrect behaviour.
Check your understanding
Answer these without looking back at the article.
- What are zero values in Go, and how do they work?
- How do zero values compare with explicit initialization or a constructor in Go?
- When should you use zero values in Go?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.