defer

This function is not defined in the built-in package. But since it is used in the example for recover I wanted to mention it.

defer is used to ensure that a function will be executed right before the funcation that calls it terminates. As Go By example mentions, other languages provide the construct finally in some contexts.

The syntax is simple:

defer func() { // ... 
}()

Or directly

defer closeFile(f)

defer takes a closure as argument. Which means that it is possible to include a wide range of elements inside it.

As noted in the recover section, if we call defer multiple times, the order in which these functions will be executed is last in, first out. So, for instance:

import (
    "fmt"
)
func main() {
    fmt.Println("Let's count backwards:")
    defer func() {
        fmt.Println(1)
    }()
    defer func() {
        fmt.Println(2)
    }()
    defer func() {
        fmt.Println(3)
    }()
}

This will output

Let's count backwards:
3
2
1