Gaaker Go

Golang Defer statement

The defer statement defers the function execution until the outer function returns.

The parameters of the delayed function will be evaluated immediately, but the function will not be called until the outer function returns.

The following is a complete code example.

package main

import "fmt"

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")
}

Below is the program output:

hello
world
< Defer statement Defer statement >