0
0
Goprogramming~10 mins

Panic behavior in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Panic behavior
Start program
Call function
Panic occurs?
NoContinue normal execution
Yes
Run deferred functions
Program crashes with panic message
The program runs normally until a panic happens, then it runs deferred functions before crashing.
Execution Sample
Go
package main
import "fmt"
func main() {
  defer fmt.Println("Deferred runs")
  panic("Something went wrong")
}
This code shows a panic happening and a deferred function running before the program crashes.
Execution Table
StepActionOutputState
1Start main functionmain started
2Set defer to print 'Deferred runs'defer set
3Call panic with message 'Something went wrong'panic triggered
4Run deferred function: print 'Deferred runs'Deferred runsdeferred function executed
5Program crashes with panic messagepanic: Something went wrongprogram stops
💡 Program stops after panic and deferred functions run
Variable Tracker
VariableStartAfter panicAfter deferFinal
panic statefalsetruetrueprogram stopped
Key Moments - 2 Insights
Why does the deferred function run even after panic?
Because Go always runs deferred functions before the program crashes due to panic, as shown in step 4 of the execution table.
Does the program continue normal execution after panic?
No, after panic and running deferred functions, the program stops immediately, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed by the deferred function?
ADeferred runs
BSomething went wrong
Cpanic: Something went wrong
DNo output
💡 Hint
Check step 4 in the execution table where the deferred function output is shown.
At which step does the panic get triggered?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in the execution table for when panic is called.
If the defer statement was removed, what would change in the output?
AThe panic message would not print
BThe program would not panic
CThe deferred message 'Deferred runs' would not print
DThe program would continue running after panic
💡 Hint
Refer to the output in step 4 where the deferred function prints its message.
Concept Snapshot
panic("message") stops normal execution immediately
Deferred functions run before program crashes
Program prints panic message and exits
Use defer to clean up before panic ends program
Panic is like a run-time error causing crash
Full Transcript
This example shows how panic works in Go. The program starts main, sets a deferred function to print a message, then calls panic with a message. When panic happens, normal execution stops, but deferred functions still run. After running deferred functions, the program crashes and prints the panic message. This behavior ensures cleanup code runs even on errors. The execution table traces each step: starting main, setting defer, triggering panic, running defer, and stopping program. Variables track panic state changing from false to true. Key moments clarify why defer runs after panic and that the program does not continue after panic. The quiz tests understanding of output, panic timing, and effect of removing defer. The snapshot summarizes panic behavior simply and clearly.