---
title: "What is the purpose of the defer keyword in Go?"  
description: "What is the purpose of the defer keyword in Go?"  
author: "Ravi Vishwakarma"  
published: 2025-04-01  
updated: 2025-04-07  
canonical: https://www.mindstick.com/forum/161399/what-is-the-purpose-of-the-defer-keyword-in-go  
category: "programming language"  
tags: ["Go Programming"]  
reading_time: 2 minutes  

---

# What is the purpose of the defer keyword in Go?

What is the [purpose](https://yourviews.mindstick.com/view/247/no-fail-policy-failing-its-purpose) of the `defer` [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) in Go?

## Replies

### Reply by Khushi Singh

The **Go programming language** includes defer for scheduling function execution following completion of the enclosing function, just before returns occur. The language implements defer for functions that require automatic completion tasks even when the surrounding function exits through any means, including resource release measures *(e.g., file closes, network disconnects)* and mutex unlocking.

Deferred functions always execute automatically, and the main purpose of defer is to ensure cleanup operations succeed regardless of errors or early function returns. The LIFO execution order of deferred function calls starts from the most recently added deferred execution to the stack of pending operations.

## The following example demonstrates how defer functions in programming:

```plaintext
package main
import "fmt"
func testDefer() {
   fmt.Println("Start of function")
   // Defer ensures this call happens last, before the function exits.
   defer fmt.Println("This will be printed last")
   fmt.Println("Middle of function")
   // The deferred function call will execute just before returning from testDefer
}
func main() {
   testDefer()
}
```

## Output

```plaintext
Start of function
Middle of function
This will be printed last
```

## Key points about defer:

- A deferred function follows a specific execution pattern that performs calls in the reverse order of deferment, which starts with the last defer statement in the function.
- Resource Management represents one primary use case of defer, which provides guaranteed final release for files, sockets, and locks in either normal or error scenarios.
- Error-handling functions execute their code when deferred, even though panic or early return occurs, thus enabling proper state management and clean-up operations.

The defer keyword serves two essential purposes in Go: through reliable resource management, and it makes programs easier to understand by guaranteeing that cleanup procedures always execute.

[**Here is everything you need to know about GO.**](https://www.mindstick.com/articles/334682/the-go-programming-language-everything-you-should-know)


---

Original Source: https://www.mindstick.com/forum/161399/what-is-the-purpose-of-the-defer-keyword-in-go

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
