This tutorial aims to demystify three essential concepts in Go programming — Defer, Panic, and Recover. These mechanisms are powerful tools in error handling and maintaining program execution flow in Go.
By the end of this tutorial, you will have a clear understanding of how to use Defer, Panic, and Recover effectively in your Go programs. You will learn how to manage function executions and control error propagation in your code.
Basic knowledge of Go programming language is recommended before going through this tutorial. Familiarity with error handling concepts in general would be helpful but not necessary.
In Go, defer
is used to ensure that a function call is performed later in a program's execution, usually for purposes of cleanup. defer
is often used where ensure
and finally
would be used in other languages.
func example() {
defer fmt.Println("world")
fmt.Println("hello")
}
In the above example, even though defer fmt.Println("world")
appears before fmt.Println("hello")
, it is executed after fmt.Println("hello")
.
Panic
is a built-in function that stops the ordinary flow of control and begins panicking. When the function F
calls panic, execution of F
stops, and any deferred functions in F
are executed normally, and then F
returns to its caller.
func example() {
defer fmt.Println("world")
panic("something bad happened")
fmt.Println("hello")
}
In this example, panic
stops the execution, defer
functions are then executed, and then the program crashes with a log message: panic: something bad happened.
Recover
is a built-in function that regains control of a panicking goroutine. Recover
is only useful inside deferred functions.
func example() {
defer func() {
if err := recover(); err != nil {
fmt.Println("Recovered in f", err)
}
}()
panic("something bad happened")
}
Here, recover
is used to catch the panic and regain control of the program. The log message will be: Recovered in f something bad happened.
package main
import "fmt"
func main() {
fmt.Println("start")
panicAndRecover()
fmt.Println("end")
}
func panicAndRecover() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered", r)
}
}()
fmt.Println("about to panic")
panic("something bad happened")
fmt.Println("done panicking")
}
This program will output:
start
about to panic
Recovered something bad happened
end
In this tutorial, we've covered the basics of defer
, panic
, and recover
in Go. These are powerful concepts that allow you to control the flow of your programs and handle errors effectively.
Solutions and explanations to these exercises will help solidify your understanding of these concepts. After you have attempted the exercises, check your solutions against those provided and revisit the sections if necessary. Happy coding!