Keyword

Go defer keyword

Schedules a call for the surrounding function’s exit, including panic unwinding. The function value and arguments are evaluated at the defer statement; deferred calls run in reverse order. It does not run at the end of an inner block. Example prints start then cleanup.

Formal Syntax & Structure

go syntaxgo
defer functionCall

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  defer fmt.Println("cleanup")  fmt.Println("start")}
Go Reference