Pairing Open with Close
Acquire and release symmetrically.
Every Open Needs a Close
Resources come in pairs: open and close, alloc and free, lock and unlock. The skill is keeping each pair balanced.
Pair Them on the Spot
The Zig habit is to write the close immediately after the open, using defer, so the pair can never drift apart.
const f = try open();
defer f.close();All lessons in this course
- What defer Guarantees
- Execution Order of Multiple defers
- defer vs errdefer in Practice
- Pairing Open with Close