errdefer for Failure Cleanup
Undo work only when something fails.
Cleanup Only on Failure
Sometimes you must undo work when an operation fails, but keep it when it succeeds. That precise case is what errdefer is for.
Meet errdefer
An errdefer schedules code to run as the scope exits, but only when the function returns an error from that point onward.
errdefer allocator.free(buffer);All lessons in this course
- Recover with catch
- Switch on Specific Errors
- errdefer for Failure Cleanup
- Wrapping and Re-throwing Errors