07 / 08

What are zero values in Go, and why do they matter?

Difficulty: 3/10
zero value initialization, struct field defaults, slice/map zero behavior

Every variable declared without an explicit value in Go is automatically assigned its type's zero value, eliminating undefined behavior found in languages like C.

Go guarantees that every declared variable has a well-defined initial value called the zero value. This design choice eliminates a whole class of bugs common in C/C++ where uninitialized variables hold garbage memory values.

Zero values by type
  1. 1

    int, float64, byte, rune → 0

  2. 2

    bool → false

  3. 3

    string → "" (empty string)

  4. 4

    pointer, slice, map, channel, function, interface → nil

  5. 5

    struct → each field gets its own zero value recursively

Zero value examples

Practical implication: you can safely use a struct before setting any field. A nil slice has len 0 and can be appended to. However, a nil map will panic on write — always initialize maps with make() before inserting keys.

Scenario Questions

0-2 years experience

  1. 1You declare a struct with an int field and a string field but don’t assign values — what do those fields contain when you print them?
  2. 2You create a slice and try to append to it without initializing it — what happens? Why?

2-5 years experience

  1. 1A user service returns an empty list of orders — the frontend shows 'No orders' but the backend logs show a nil slice. Why is this a problem, and how do you fix it?
  2. 2Your API endpoint returns a struct with a zero-valued Price field, but the client expects null for unset prices. How do you handle this without breaking existing clients?

5-8 years experience

  1. 1You’re designing a caching layer that stores structs with optional metadata fields — how do you distinguish between a field being intentionally unset versus zero-valued, and what performance or correctness tradeoffs do you consider?
  2. 2A distributed system uses zero-valued timestamps to indicate 'not set', but race conditions cause some nodes to interpret zero as 'epoch time'. How do you redesign this to avoid ambiguity at scale?

8+ years experience

  1. 1Your company’s legacy Go microservices rely heavily on zero values to represent optional fields, but now you’re migrating to a schema-driven API gateway that treats zero as explicit. How do you plan the migration without breaking hundreds of downstream services?
  2. 2You’re designing a new data pipeline where zero values in metrics are indistinguishable from missing data — how do you architect the type system and serialization layer to support both semantic clarity and backward compatibility across teams?

Follow-up Questions

  • Can you give an example where relying on zero value led to a bug you had to debug?
  • How does Go’s zero value compare to null in other languages like Java or Python?
  • What happens if you try to use a zero-value map without initializing it?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.