Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
defer fmt.Print(1)
defer fmt.Print(2)
defer fmt.Print(3)
fmt.Print("go")
}go321
- Expecting
go123: deferred calls run in reverse order, the last deferred first, so3prints before2and1. - Expecting the digits before
go:deferonly schedules the call; it runs whenmainreturns, afterfmt.Print("go").
- What would
x := 1; defer fmt.Print(x); x = 2print, and why? - Where is
defertypically used in real code?