0
0
Goprogramming~10 mins

Why defer is used in Go - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why defer is used
Start function
Execute code before defer
Register defer statement
Execute remaining code
Function about to return
Run deferred calls in LIFO order
Function returns
The function runs normally, but defer statements are saved and run just before the function returns, in reverse order.
Execution Sample
Go
func example() {
    defer fmt.Println("deferred call")
    fmt.Println("normal call")
}
This code prints "normal call" first, then "deferred call" after the function finishes.
Execution Table
StepActionOutputDeferred Calls Stack
1Start function example()[]
2Register defer fmt.Println("deferred call")[fmt.Println("deferred call")]
3Execute fmt.Println("normal call")normal call[fmt.Println("deferred call")]
4Function about to return, run deferred callsdeferred call[]
5Function returns[]
💡 Function ends after running deferred calls in last-in-first-out order
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Deferred Calls Stack[][fmt.Println("deferred call")][fmt.Println("deferred call")][][]
Key Moments - 2 Insights
Why does the deferred call run after the normal print statement?
Because deferred calls are saved when registered and only run after the function finishes, as shown in execution_table step 4.
What happens if there are multiple defer statements?
They run in last-in-first-out order just before the function returns, meaning the last defer registered runs first.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3?
Adeferred call
Bnothing
Cnormal call
Dfunction returns
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step are deferred calls executed?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the Action and Output columns in execution_table for when deferred calls run.
If we add another defer before the existing one, how does the Deferred Calls Stack change after step 2?
AIt contains both defers but the last added is on top
BIt contains both defers in order of registration
CIt contains only the first defer
DIt becomes empty
💡 Hint
Deferred calls run in last-in-first-out order, so the stack grows with newest on top.
Concept Snapshot
defer schedules a function call to run after the current function finishes.
Deferred calls run in last-in-first-out order just before return.
Use defer to clean up resources like files or locks.
Deferred calls run even if the function panics.
Syntax: defer functionCall()
Full Transcript
In Go, defer is used to delay a function call until the surrounding function finishes. When you write defer before a function call, Go saves that call and runs it after the current function returns. This is useful for cleanup tasks like closing files or unlocking resources. Deferred calls run in reverse order if there are multiple. For example, if you defer printing a message, the normal code runs first, then the deferred print happens last. This ensures important cleanup code always runs, even if the function exits early or due to an error.