Defer

A defer statement defers the execution of a function until the surrounding function returns.

The deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

defer.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. defer fmt.Println("world")
  5. fmt.Println("hello")
  6. }