0Pricing
Zig Academy · Lesson

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

  1. What defer Guarantees
  2. Execution Order of Multiple defers
  3. defer vs errdefer in Practice
  4. Pairing Open with Close
← Back to Zig Academy