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…
Author: admin
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…
Example of Recover Function in Go (Golang)
Overview Go provides a built-in function recover for recovering from a panic. Below is the signature of this function defer function is the only function that is called after the panic. So…
How to create panic in Go (Golang)
Overview Panic is similar to the exception in golang. Panic is meant to exit from a program in abnormal conditions. Panic can occur in a program in two ways Runtime error in…
Panic stack trace in Go (Golang)
Overview debug package of golang provides a StackTrace function that can be used print the stack trace of the panic in the recover function Example Let’s see a program for that Output In…
Format a message without printing in Go (Golang)
Overview Sprintf function of fmt package can be used to format the string without printing it. It is similar to Printf function with the only difference between the two being Printf formats…
Recover function return value in Go (Golang)
Overview The recover function returns the value which was passed to the panic function. Therefore it is a good practice to check the return value of the recover function. If the return value is…