Complete the code to defer printing "First".
package main import "fmt" func main() { [1] fmt.Println("First") fmt.Println("Second") }
The defer keyword delays the execution of the function until the surrounding function returns.
Complete the code to print "Third" last using defer.
package main import "fmt" func main() { defer fmt.Println("[1]") fmt.Println("First") fmt.Println("Second") }
The deferred call prints "Third" after the other prints because defer delays execution until the function ends.
Fix the error in the defer statement to print "Done" last.
package main import "fmt" func main() { defer fmt.Println([1]) fmt.Println("Start") }
Strings in Go must be enclosed in double quotes. Using "Done" correctly passes a string literal.
Fill both blanks to defer two prints so "Second" prints before "First".
package main import "fmt" func main() { defer fmt.Println([1]) defer fmt.Println([2]) fmt.Println("Start") }
Deferred calls run in last-in-first-out order. The second defer prints "Second" before the first defer prints "First".
Fill all three blanks to defer prints so output order is: Start, Third, Second, First.
package main import "fmt" func main() { fmt.Println("Start") defer fmt.Println([1]) defer fmt.Println([2]) defer fmt.Println([3]) }
The immediate print shows "Start" first. Deferred prints run in reverse order: last defer prints "Third", then the middle defer prints "Second", then the first defer prints "First".