In this program, we're introduced to the defer keyword in Go. Let's break it down:
func foo() {
fmt.Println("I am foo")
}This is a straightforward function that, when called, will print the string "I am foo" to the console.
func bar() {
fmt.Println("I am bar")
}Similarly, this function, when called, will print the string "I am bar" to the console.
In the main function, the following calls are made:
defer foo()
bar()Here's a breakdown of what happens:
-
defer foo(): Thedeferkeyword in Go is used to ensure that a function call is performed later in a program's execution, usually for purposes of cleanup. Typically, deferred functions are executed in reverse order of their appearance. In this case, thefoofunction has been deferred, which means it won't be executed immediately—it'll wait until the surrounding function (mainin this case) completes its execution. -
bar(): This is a normal function call. It will be executed immediately.
Given the ordering in the main function, the output of the program will be:
I am bar
I am foobar prints its message first, even though its call appears after foo in the code. This is because foo has been deferred, so it waits until the end of the main function to execute.
- Run the following
$ go run main.go
I am bar
I am foo