A defer statement is executed just before return from a function. It means within a function it doesn’t follow the normal execution steps or i,e it doesn’t follow the sequential execution of the statements. Thus a defer statement is executed as the last statement of a function, though it is not present at the end of the function.
import “fmt”
func main() {
defer fmt.Println(“Hello-3”)
fmt.Println(“Hello-1”)
fmt.Println(“Hello-2”)
}
Hello-1
Hello-2
Hello-3
Here, it looks like the output will be in the order of Hello-3, Hello-1 and Hello-2, but as the Hello-3 is in a defer statement, hence it will execute at the end of the main function call and will be printed as last output.
Defer statements are generally used in situations like:–
To ensure that a opened file is closed properly, once we are done with the updation of that file or i,e just before coming out of that function.
func file_update(file1) {
defer file_close_function(file1) // Last function to execute.
file_open_and_update_function_1(file1)
file_open_and_update_function_2(file1)
file_open_and_update_function_3(file1)
}
In above example, we have three functions lets named as f1, f2 and f3, which are trying to open and update same file “file1” . It might happen all the 3 functions will completely execute and return normally or only f1 will execute completely and will return without executing f2 and f3, because of some code issue or a panic.
In either of the cases, just before coming out of the function file_update(), we have to close the file “file1″ properly and then return. Thus the above statement i,e ” defer file_close_function(file1) ” will close the file “file1” just before coming out of the file_update() function.
Defer statement is also used in situations like, to handle a panic function or to do some clean-up stuffs just before coming out of a function.
Properties of defer statements and defer functions:—
1. When there are multiple defer statements and functions in a program, they are executed in LIFO(Last-In and First-Out) order.
2. Considering the above statement, The arguments of a defer statement or function are evaluated at the time when we push them into the LIFO-stack. check below example.
import “fmt”
func func1(a int) {
fmt.Println(“Value inside function is”, a)
}
func main() {
a := 100
defer fmt.Println(“value in defer-1”, a)
a = a-10
fmt.Println(“value in print-1”, a)
defer fmt.Println(“value in defer-2”, a)
defer func1(a)
a = a-10
fmt.Println(“value in print-2”, a)
}
value in print-1 90
value in print-2 80
Value inside function is 90
value in defer-2 90
value in defer-1 100
As above, the defer-1 is pushed into the LIFO-stack with a=100.
Next defer-2 is pushed into the LIFO-stack with a=a-10 i,e a=90
next func1 is pushed into the LIFO-stack with a=90.
Now at the end of the main() function execution, when we execute the LIFO-stack, first it will execute func1(As func1 was the last in) with a=90, next defer-2 is executed with a=90 and finally defer-1 is executed with a=100.
Other use cases of defer:—
1. use of defer in panic-recover .
2. use of defer in waitgroup.
i love this exceptional article
You have brought up a very wonderful details, thank you for the post.
I am actually thankful to the holder of this site who
has shared this enormous post at at this time.
Thanks Bertha, for the nice comments..
I think this is one of the most vital information for me. And i
am glad reading your article. But want to remark on few general
things, The web site style is great, the articles is really great : D.
Good job, cheers 0mniartist asmr