0
0
Goprogramming~10 mins

Defer execution order in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Defer execution order
Start function
Execute statements
Encounter defer statement
Store deferred call on stack
Continue executing
Function end reached
Pop deferred calls from stack
Execute deferred calls in LIFO order
Function returns
When Go runs a function, deferred calls are saved and run last in reverse order, just before the function ends.
Execution Sample
Go
package main
import "fmt"

func main() {
  defer fmt.Println("First")
  defer fmt.Println("Second")
  fmt.Println("Hello")
}
This code prints "Hello" first, then deferred prints in reverse order: "Second", then "First".
Execution Table
StepActionDeferred StackOutput
1Start main function[]
2defer fmt.Println("First")[First]
3defer fmt.Println("Second")[First, Second]
4fmt.Println("Hello")[First, Second]Hello
5End of main reached, start deferred calls[First, Second]Hello
6Pop and execute deferred call: Second[First]Hello Second
7Pop and execute deferred call: First[]Hello Second First
8main function returns[]Hello Second First
💡 Function ends, all deferred calls executed in last-in-first-out order.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 7Final
Deferred Stack[][First][First, Second][First][][]
Output"""""""Hello Second ""Hello Second First ""Hello Second First "
Key Moments - 2 Insights
Why do deferred calls run after the function's main code finishes?
Deferred calls are stored on a stack and only run after the function reaches its end, as shown in steps 5-8 in the execution_table.
Why does the last deferred call run first?
Deferred calls run in last-in-first-out order, so the most recently deferred call executes first, as seen in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the deferred stack after step 3?
A[Second, First]
B[First, Second]
C[First]
D[]
💡 Hint
Check the 'Deferred Stack' column at step 3 in the execution_table.
At which step does the output first show "Second"?
AStep 6
BStep 4
CStep 7
DStep 8
💡 Hint
Look at the 'Output' column in the execution_table and find when "Second" appears.
If we add another defer after step 3, where would it appear in the deferred stack?
AAt the bottom of the stack
BIt replaces the first deferred call
CAt the top of the stack
DIt runs immediately
💡 Hint
Deferred calls are pushed onto the stack; new defers go on top as shown in steps 2 and 3.
Concept Snapshot
Go's defer statements save calls to run later.
Deferred calls run after the function ends.
They run in reverse order (last-in-first-out).
Use defer to delay cleanup or final actions.
Deferred calls stack up and execute just before return.
Full Transcript
In Go, when you use defer, the function call is saved and runs only after the surrounding function finishes. Each defer adds a call to a stack. When the function ends, Go pops calls off this stack and runs them in reverse order. For example, if you defer printing "First" and then "Second", the program prints "Hello" first, then "Second", then "First". This happens because deferred calls run last-in-first-out. This helps with cleanup tasks that must happen after the main work is done.