Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Defer early returns

package main

import "fmt"

func main() {
	run(false)
	fmt.Println("--------")
	run(true)
}

func run(early bool) {
	fmt.Println("first")
	defer fmt.Println("do at the end")
	fmt.Println("second")
	if early {
		return
	}

	fmt.Println("last")
}
first
second
last
do at the end
--------
first
second
do at the end