Defer Keyword in Golang

09 Jun, 2021
In Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. In other words, defer function or method call arguments evaluate instantly, but they don’t execute until the nearby functions returns. You can create a deferred method, or function, or anonymous function by using the defer keyword.
Syntax:
// Function
defer func func_name(parameter_list Type)return_type{
// Code
}

// Method
defer func (receiver Type) method_name(parameter_list){
// Code
}

defer func (parameter_list)(return_type){
// code
}()
Important Points:
In Go language, multiple defer statements are allowed in the same program and they are executed in LIFO(Last-In, First-Out) order as shown in Example 2.
In the defer statements, the arguments are evaluated when the defer statement is executed, not when it is called.
Defer statements are generally used to ensure that the files are closed when their need is over, or to close the channel, or to catch the panics in the program.
Let us discuss this concept with the help of an example:
Example 1: 
Go
// Go program to illustrate the// concept of the defer statementpackage main import "fmt" // Functionsfunc mul(a1, a2 int) int {     res := a1 * a2    fmt.Println("Result: ", res)    return 0} func show() {    fmt.Println("Hello!, GeeksforGeeks")} // Main functionfunc main() {     // Calling mul() function    // Here mul function behaves    // like a normal function    mul(23, 45)     // Calling mul()function    // Using defer keyword    // Here the mul() function    // is defer function    defer mul(23, 56)     // Calling show() function    show()}
Output: 
Result:  1035
Hello!, GeeksforGeeks
Result:  1288
Explanation: In the above example we have two functions named mul() and show(). Where show() function is called normally in the main() function, mul() function is called in two different ways:
First, we call mul function normally(without the defer keyword), i.e, mul(23, 45) and it executes when the function is called(Output: Result : 1035 ).
Second, we call mul() function as a defer function using defer keyword, i.e, defer mul(23, 56) and it executes(Output: Result: 1288 ) when all the surrounding methods return.
Example 2:
Go
// Go program to illustrate// multiple defer statements, to illustrate LIFO policypackage main import "fmt" // Functionsfunc add(a1, a2 int) int {    res := a1 + a2    fmt.Println("Result: ", res)    return 0} // Main functionfunc main() {     fmt.Println("Start")     // Multiple defer statements    // Executes in LIFO order    defer fmt.Println("End")    defer add(34, 56)    defer add(10, 10)}
Output:
Start
Result:  20
Result:  90
End
 
competing23invain
Go-Keywords
Golang
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to concatenate two strings in Golang
time.Sleep() Function in Golang With Examples
strings.Contains Function in Golang with Examples
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
Golang Maps
Time Formatting in Golang
Interfaces in Golang
Different Ways to Find the Type of Variable in Golang
How to Parse JSON in Golang?