Introduction to Golang defer
In Go language, defer allows stopping the execution of one function till the other functions and manipulations next to it have not been done. For example, if we are doing any mathematical calculation and in that case, we wanted to do all addition first then do the multiplication, then we can put multiplication function call as the defer multiplication and then next to it all the additional functions will be called. defer is the keyword in the go language, and it main use it to delay the execution of the function called the defer function and allows the execution of a nearby statement and functions first.
Syntax
In the below, we have given a simple syntax for explaining the defer; we can explain the below syntax in the following way.
- The keyword defers written before the name of the function, which allows other functions and statements to execute first then this function.
- Next, we have written some statements; these statements will execute before the function called as the defer.
- In the same way, any function written after the defer’s function will be executed first, and once done all the functions, then the defer function will execute.
Please see the below syntax for a better understanding.
defer function-name
Statement 1
Statement 2
Function 1
Function 2
How does defer Work in Go language?
Before going to discuss the working of the defer, we need to understand the purpose and importance of the defer key; suppose we have many functions and statements to execute, but we want a particular function to wait for the execution of all other functions and statements then we will use the defer keyword. We can discuss the working of the defer with the help of the below points.
- In Go language defer allows us to execute the codes in the LIFO, which means last in, first out.
- So we can write multiple defer functions with a defer keyword, then these functions will execute the last defer function first, which is in the LIFO manners.
- The most important use case is that you are going to open a file, and you did some modification on the file, so after modification to ensure that the file was properly closed, we defer.
- In a very simple way, we can say in defer, the defer function will not be called when actually we made a call to it; rather, the call be made when the statement next to the defer function executed and completed.
- Suppose you connected to any database and you perform some operation of the database; after that, the closing of the database connection is important, so to ensure if the database connection was properly closed or not, we use the defer key, simply we can put the logic for database connection closing and will be called it as the defer function which will get a call at the end of all the operations.
Examples to Implement Golang defer.
In the below, we have given some of the examples to explain the working of the defer in the go language. We have given some examples where we are calling function as the defer, and after that, we are calling some functions and statements. In the examples, we can see that defer functions are always getting a call at the end of the functions. In case we want to test the examples, we can create a file with the name defer.go and copy and paste the examples on the file and run the command go run defer.go and see the outputs.
Example #1
Code:
package main
import "fmt"
func multiply(a1, a2 int) int {
res := a1 * a2
fmt.Println("Result: ", res)
return 0
}
func show() {
fmt.Println("Hello!, How are you friends")
}
func tellYourName(){
fmt.Println("Hello friend my name is , Ranjan")
}
func main() {
multiply(21, 45)
defer multiply(21, 56)
show()
tellYourName()
}
Output:
Example #2
Code:
package main
import "fmt"
func multiply(a1, a2 int) int {
res := a1 * a2
fmt.Println("Result: ", res)
return 0
}
func show() {
fmt.Println("Hello!, How are you friends")
}
func tellYourName(){
fmt.Println("Hello friend my name is , Ranjan")
}
func endTheTalk(){
fmt.Println("This is was the end of the talk friends")
}
func main() {
multiply(21, 45)
defer multiply(21, 56)
show()
tellYourName()
endTheTalk()
}
Output:
Example #3
Code:
package main
import "fmt"
func addition(p1, p2 int) int {
sum := p1 + p2
fmt.Println("The result for addition is : ", sum)
return 0
}
func main() {
fmt.Println("Here we are starting the execution")
defer fmt.Println("This is the End of the addition")
defer addition(50, 66)
defer addition(30, 40)
}
Output:
Example #4
Code:
package main
import "fmt"
func multiply(p1, p2 int) int {
sum := p1 * p2
fmt.Println("The result for multiplication is : ", sum)
return 0
}
func main() {
fmt.Println("Here we are starting the execution")
defer fmt.Println("This is the End of the multiplications")
defer multiply(50, 66)
defer multiply(30, 40)
defer multiply(23, 12)
defer multiply(33, 11)
}
Output:
Example #5
Code:
package main
import "fmt"
func addition(p1, p2 int) int {
sum := p1 + p2
fmt.Println("The result for addition is : ", sum)
return 0
}
func multiplication(p1, p2 int) int {
sum := p1 * p2
fmt.Println("The result for Multiplication is : ", sum)
return 0
}
func main() {
fmt.Println("Here we are starting the execution")
defer fmt.Println("This is the End of the arithmetic operation")
defer addition(50, 66)
defer multiplication(30, 40)
}
Output:
Conclusion
From this tutorial, we learned the basic concept of the defer key and its uses in the go language. We learned the working of the defer along with the importance of the defer. We saw syntax and examples of the defer, which explain the main uses and importance.
Recommended Article
This is a guide to Golang defer. Here we discuss the introduction, syntax, and working of Golang defer along with different examples and code implementation. You can also go through our other suggested articles to learn more –