Overview Defer as the name suggests is used to defer the cleanup activities in a function. These cleanup activities will be performed at the end of the function. defer can be used…
Tag: go
Defer and Methods in Go (Golang)
Overview defer statement is also applicable for methods in a similar way it is applicable to functions. Let’s see an example Example In the above program, we do defer file.Close() after opening…
Defer function and Named Return Values in Go (Golang)
Overview In case of named return value in the function, the defer function can read as well as modified those named return values. If the defer function modifies the name return value…
Multiple defer functions in Go (Golang)
Overview There can be two cases of multiple defer functions Multiple Defer function within a particular function Multiple Defer function in different functions Before we see an example for both let’s see…
What happens during panic in Go (Golang)
Overview Let’s understand what happens when panic happens in a program. Imagine a function call from main function to f1 function to f2 function main->f1->f2 Now let’s say that panic happens in…
How does defer works in Go (Golang)
Overview When the compiler encounters a defer statement in a function it pushes it onto a list. This list internally implements a stack-like data structure. All the encountered defer statement in the same function…
Custom Function in defer in Go (Golang)
Overview We can also call a custom function in defer. Let’s see a example for that Example Output In the above program there is a defer statement calling the custom function named…
Evaluation of defer arguments in Go (Golang)
Overview defer arguments are evaluated at the time defer statement is evaluated Let’s see a program for that Example Output In the above program when the defer statement was evaluated the value…
Inline Function in Defer in Go (Golang)
Overview It is also possible to have an inline function with defer. Example Let’s see an example of that. Output In the above code we have defer with a inline function This…
Use Case of defer function in Go (Golang)
Overview Defer as the name suggests is used to defer the cleanup activities in a function. These cleanup activities will be performed at the end of the function. This cleanup activities will…