Program stability means the program runs smoothly without crashing or unexpected errors, even when faced with unusual or wrong inputs.
Click to reveal answer
beginner
How does error handling contribute to program stability in Go?
Error handling lets the program detect problems early and respond properly, preventing crashes and keeping the program running safely.
Click to reveal answer
intermediate
What is the purpose of defer in Go for stability?
The defer keyword schedules a function to run later, usually to clean up resources like files or connections, helping avoid leaks and errors.
Click to reveal answer
intermediate
Why is panic and recover important for program stability?
Panic stops the program when something bad happens, but recover can catch that panic and let the program continue safely instead of crashing.
Click to reveal answer
beginner
How does testing improve program stability?
Testing finds bugs before users do, so fixing them early keeps the program stable and reliable over time.
Click to reveal answer
What does the defer keyword do in Go?
ARuns a function later, usually for cleanup
BStops the program immediately
CHandles errors automatically
DStarts a new goroutine
✗ Incorrect
defer schedules a function to run after the current function finishes, often used to release resources.
Which Go feature helps catch a panic and prevent a program crash?
Apanic
Bdefer
Cgo routine
Drecover
✗ Incorrect
recover can catch a panic inside a deferred function and allow the program to continue running.
Why is error handling important for program stability?
AIt helps detect and manage problems before they crash the program
BIt makes the program run faster
CIt automatically fixes bugs
DIt creates new errors
✗ Incorrect
Handling errors properly prevents unexpected crashes and keeps the program stable.
What is a common use of panic in Go?
ATo start a new thread
BTo handle normal errors
CTo stop the program when a serious error occurs
DTo defer a function
✗ Incorrect
panic is used to stop the program immediately when something unrecoverable happens.
How does testing help with program stability?
ABy increasing the program size
BBy finding bugs before the program runs in production
CBy removing all errors automatically
DBy making the program run slower
✗ Incorrect
Testing helps find and fix bugs early, which keeps the program stable and reliable.
Explain how Go's defer, panic, and recover work together to improve program stability.
Think about how Go handles errors and resource cleanup safely.
You got /3 concepts.
Describe why proper error handling is essential for keeping a Go program stable.
Consider what happens if errors are ignored.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using recover() in Go programs?
easy
A. To declare a new variable
B. To start a new goroutine
C. To catch a panic and prevent the program from crashing
D. To handle normal errors returned by functions
Solution
Step 1: Understand panic and recover
Panic causes a program to stop running immediately unless recovered.
Step 2: Role of recover()
Recover is used inside a deferred function to catch the panic and allow the program to continue.
Final Answer:
To catch a panic and prevent the program from crashing -> Option C
Quick Check:
recover() catches panic = C [OK]
Hint: recover() only works inside deferred functions [OK]
Common Mistakes:
Thinking recover() handles normal errors
Using recover() outside defer
Confusing recover() with panic()
2. Which of the following is the correct syntax to defer a function call in Go?
easy
A. defer fmt.Println("Hello")
B. defer = fmt.Println("Hello")
C. defer: fmt.Println("Hello")
D. defer -> fmt.Println("Hello")
Solution
Step 1: Recall defer syntax
In Go, defer is a keyword placed before a function call to delay its execution.
Step 2: Check each option
Only defer fmt.Println("Hello") uses 'defer' correctly before the function call without extra symbols.
Final Answer:
defer fmt.Println("Hello") -> Option A
Quick Check:
defer + function call = D [OK]
Hint: defer must be followed directly by a function call [OK]
Common Mistakes:
Adding '=' after defer
Using ':' or '->' after defer
Not calling a function after defer
3. What will be the output of this Go program?
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")
}
medium
A. Start
panic: Something went wrong
B. Start
End
Recovered from panic: Something went wrong
C. Start
End
D. Start
Recovered from panic: Something went wrong
Solution
Step 1: Trace program execution
"Start" is printed first. Then panic is called, which triggers the deferred function.
Step 2: Deferred function recovers panic
The deferred anonymous function calls recover(), catches the panic, and prints the recovery message. Execution does not continue after panic call, so "End" is not printed.
Final Answer:
Start
Recovered from panic: Something went wrong -> Option D
Quick Check:
panic caught by recover = A [OK]
Hint: Deferred recover runs on panic, then execution continues after panic [OK]
Common Mistakes:
Thinking code after panic does not run
Missing recover() inside defer
Confusing panic message with normal print
4. Identify the error in this Go code that tries to recover from a panic:
package main
import "fmt"
func main() {
if r := recover(); r != nil {
fmt.Println("Recovered:", r)
}
panic("Oops")
}
medium
A. recover() must be called inside a deferred function
B. panic() must be called before recover()
C. recover() cannot be used with panic()
D. fmt.Println() cannot be used inside if statement
Solution
Step 1: Understand recover usage
recover() only works if called inside a deferred function; here it is called directly in main.
Step 2: Analyze code flow
Since recover() is not deferred, it cannot catch the panic, so the program will crash.
Final Answer:
recover() must be called inside a deferred function -> Option A
Quick Check:
recover inside defer = B [OK]
Hint: Always call recover() inside defer to catch panics [OK]
Common Mistakes:
Calling recover() directly without defer
Thinking recover() catches errors, not panics
Placing panic before recover without defer
5. You want to write a stable Go program that logs a message when a panic occurs but continues running. Which code snippet correctly implements this behavior?
hard
A. func main() {
if r := recover(); r != nil {
fmt.Println("Panic caught:", r)
}
panic("Crash")
fmt.Println("Program continues")
}
B. func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic caught:", r)
}
}()
panic("Crash")
fmt.Println("Program continues")
}
C. func main() {
panic("Crash")
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic caught:", r)
}
}()
fmt.Println("Program continues")
}
D. func main() {
defer fmt.Println("Program continues")
panic("Crash")
}
Solution
Step 1: Check defer and recover placement
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic caught:", r)
}
}()
panic("Crash")
fmt.Println("Program continues")
} correctly defers a function that calls recover() to catch panic and print a message.
Step 2: Verify program flow
After panic, deferred recover runs, preventing crash and allowing program to continue, executing code after panic.
Final Answer:
Uses defer func() with recover() before panic("Crash") -> Option B
Quick Check:
defer + recover to catch panic = A [OK]
Hint: Defer recover before panic to handle it safely [OK]
Common Mistakes:
Calling recover() outside defer
Placing defer after panic
Not handling panic at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.