Defer

Keyword · Development

Definitions

  1. A Go language keyword that schedules a function call to be executed when the enclosing function returns, regardless of how it returns (normal return, panic, or early exit). Commonly used for cleanup operations like closing files, releasing locks, and flushing buffers. Deferred calls execute in LIFO order.

    In plain English: A Go command that says 'run this cleanup code later, right before the function ends,' so you never forget to close files or free resources.

    Example: "Open the file, defer the close, and never worry about resource leaks again."

Related Terms