Section 11 of 11
Gotchas
The list worth flashcarding. Each one has bitten everybody.
The nil interface trap
go.dev playground
Try it: This is the #1 Go interview question. An interface is (type, value) — it is only nil when BOTH are nil.
Slices share a backing array
go.dev playground
Try it: Append past cap and the aliasing silently stops. That inconsistency is the bug.
- nil interface != nil pointer — an interface is (type, value).
- Slices share backing arrays until append exceeds cap. Use copy() or slices.Clone to detach.
- defer arguments are evaluated immediately; defer in a loop leaks.
- Unused imports and unused locals are compile errors.
- Capitalization is visibility — including for encoding/json and reflection.
- No circular imports, ever.
- internal/ is enforced; pkg/ is just a convention.
- Zero values are usable: a var of a struct type is ready to go, but a nil map panics on write.
- Range over a map has randomized order, deliberately.
- Since Go 1.22 loop variables are per-iteration. Older code and older blog posts assume otherwise.
- Struct assignment copies. Method receivers copy unless they are pointers.