0
0
Goprogramming~10 mins

Program stability concepts in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Program stability concepts
Start Program
Initialize Variables
Run Main Logic
Check for Errors
Handle Error
Recover or Exit
End Program
This flow shows how a Go program runs, checks for errors, and handles them to keep the program stable.
Execution Sample
Go
package main
import "fmt"
func main() {
  defer func() {
    if r := recover(); r != nil {
      fmt.Println("Recovered from panic:", r)
    }
  }()
  fmt.Println("Start")
  panic("Something went wrong")
  fmt.Println("End")
}
This Go program uses defer and recover to catch a panic and keep the program stable.
Execution Table
StepActionEvaluationResultOutput
1Start main functionNo errorProgram starts
2Set defer function for recoveryDefer setRecovery ready
3Print "Start"No errorPrintedStart
4Call panic("Something went wrong")Panic triggeredProgram panics
5Defer function runs due to panicRecover calledPanic caughtRecovered from panic: Something went wrong
6Print "End"No errorPrinted
7End main functionProgram endsNormal exit
💡 Program ends normally after recovering from panic, preventing crash.
Variable Tracker
VariableStartAfter Step 4After Step 5Final
r (recover)nilnil (panic active)"Something went wrong" (panic caught)"Something went wrong"
Key Moments - 3 Insights
Why does the program not crash after panic is called?
Because the deferred function uses recover() to catch the panic, as shown in step 5 of the execution_table, allowing the program to continue.
What happens if recover() is not called inside defer?
The panic would not be caught, and the program would crash immediately after step 4, as no recovery is done.
Why is defer used before panic is called?
Defer sets up the recovery function to run when panic happens, ensuring the program can catch the panic and stay stable.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of variable r after step 5?
Apanic active
Bnil
C"Something went wrong"
Dundefined
💡 Hint
Check variable_tracker column 'After Step 5' for r's value.
At which step does the program catch the panic and prevent crashing?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at execution_table row where recover() is called.
If the defer function was removed, what would happen at step 4?
AProgram would recover automatically
BProgram would crash and stop
CProgram would ignore panic
DProgram would print "Recovered from panic"
💡 Hint
Refer to key_moments about what happens without recover.
Concept Snapshot
Go programs can panic on errors.
Use defer with recover() to catch panics.
This prevents crashes and keeps programs stable.
Defer runs after panic triggers.
Recover returns panic info and resumes execution.
Full Transcript
This visual trace shows a Go program starting, setting a deferred recovery function, printing a start message, then panicking. The panic triggers the deferred function, which calls recover to catch the panic and print a recovery message. This prevents the program from crashing and allows it to end normally. Variables like r hold the panic info during recovery. Key points include why defer and recover are needed to keep the program stable and what happens if they are missing.