Welcome To Golang By Example

Can defer be used inside main function in Go (Golang)

Table of Contents

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 inside the main function as well. Let’s see an example for that

Example

package main
import "fmt"
func main() {
    defer test()
    fmt.Println("Executed in main")
}
func test() {
    fmt.Println("In Defer")
}

Output

Executed in main
In Defer

In the above program there is a defer statement calling the custom function named test. As seen from the output, the test function is called after everything in the main is executed and before main returns. That is why

Executed in main

is printed before

In Defer

The above function also that it is perfectly ok to use defer in the main function as well.