Junior · Go foundations

Zero values and safe APIs

Is every zero value ready for every operation?

Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.

EXAMPLE · FRAGMENT UNLESS A COMPLETE PROGRAM IS SHOWN

go
var s []int
s = append(s, 1)
m := make(map[string]int)
m["x"] = 1
Reveal a strong answer
  1. A declaration always initializes storage. Numeric values start at zero; pointers, maps, slices, channels and interfaces start nil. A struct recursively receives zero-valued fields.

  2. Usable does not mean every operation succeeds: appending to a nil slice works, assigning a nil map panics, and sending to a nil channel blocks. A zero mutex is ready; a service needing configuration should say so in a constructor.

  3. Design a type so its zero value is useful where reasonable. When setup is required, validate it at the boundary and document ownership instead of relying on hidden initialization.

Common trap

“Everything is usable because Go has zero values” hides invalid operations on nil maps and channels.

Try it

Compare appending to var s []int with assigning m["x"] = 1 after var m map[string]int.

Worked solution

append returns a valid slice containing the value. The map assignment panics until make or a map literal supplies a map. Test both the documented nil behavior and the ordinary path.

Change the constraint

How would your type distinguish an omitted limit from an explicitly configured zero?

How did you do?

Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.

Not marked. Progress stays on this browser, separate from course completion.

Primary references

Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.