defer
defer runs a call when the function returns, in last in first out order. Great for cleanup.
Example
package main
import "fmt"
func main() {
defer fmt.Println("last")
defer fmt.Println("second")
fmt.Println("first")
}Result
first second last
Exercise
Run at exit.
fmt.Println("x")Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.