In Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. Or in other words, defer function or method call arguments evaluate instantly, but they 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 executed, not when they called.
- Defer statements are generally used to ensure that the files are closed when your work is finished with them, 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 program to illustrate the // concept of the defer statement package main import "fmt" // Functions func mul(a1, a2 int) int { res := a1 * a2 fmt.Println("Result: ", res) return 0 } func show() { fmt.Println("Hello!, GeeksforGeeks") } // Main function func 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 as mul() and show() function. Where show() function call normally in the main() function, but we call mul() function in two different ways:
- First, we call mul function like the normal function, i.e, mul(23, 45) and executes when the function 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 program to illustrate // multiple defer statements package main import "fmt" // Functions func add(a1, a2 int) int { res := a1 + a2 fmt.Println("Result: ", res) return 0 } // Main function func 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
Recommended Posts:
- Golang program that uses defer keyword
- Golang program that uses fallthrough keyword
- Difference between var keyword and short declaration operator in Golang
- Range Keyword in Golang
- How to instantiate Struct using new keyword in Golang?
- var keyword in Go
- How to convert a slice of bytes in uppercase in Golang?
- math.Lgamma() Function in Golang with Examples
- math.Float64bits() Function in Golang With Examples
- How to check equality of slices of bytes in Golang?
- atomic.AddInt64() Function in Golang With Examples
- atomic.StoreInt64() Function in Golang With Examples
- reflect.FieldByIndex() Function in Golang with Examples
- string.Contains Function in Golang with Examples
- bits.Sub() Function in Golang with Examples
- How to convert a slice of bytes in lowercase in Golang?
- io.PipeWriter.CloseWithError() Function in Golang with Examples
- Import in GoLang
- time.Round() Function in Golang With Examples
- How to add a method to struct type in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

