Introduction
Defer is used to delay a function call until the surrounding function finishes. It helps clean up resources like files or locks automatically.
Closing a file after reading or writing to it.
Releasing a lock after finishing a critical section.
Closing a network connection when done.
Undoing temporary changes before a function ends.
Ensuring cleanup code runs even if an error happens.