Defer


Defer is called when the function exits. Defer is a LIFO stack.

package main

import (
    "fmt"
)

func defer_test() {
    defer fmt.Println("1st defer")
    defer fmt.Println("2nd defer")
    defer fmt.Println("3rd defer")

    fmt.Println("Just entered defer_test. I will now exit.")
}

func main() {
    fmt.Println("Entering defer_test")
    defer_test()
    fmt.Println("Back from defer_test")
}

Output

Entering defer_test
Just entered defer_test. I will now exit.
3rd defer
2nd defer
1st defer
Back from defer_test

results matching ""

    No results matching ""