Welcome To Golang By Example

Custom Function in defer in Go (Golang)

Table of Contents

Overview

We can also call a custom function in defer. Let’s see a 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 shows that it is perfectly ok to use defer in the main function as well.