Go code
panic("unexpected state")Line-by-line explanation
- Panic is called with a message.
- Normal execution stops.
- Go prints a stack trace.
Expected output
panic: unexpected stateAdditional stack-trace lines normally follow.
Use returned errors for expected failures such as invalid input or missing files. Do not use panic as a replacement for if err != nil.
Tiny Practice
Starter scenario
Choose the normal mechanism for a missing user file: panic or returned error.
Go code
choice := "CHANGE_ME"
fmt.Println(choice)Line-by-line explanation
- The answer is stored as text.
- Replace CHANGE_ME.
Expected output
(complete the starter first)The starter intentionally contains CHANGE_ME.
Hint
- A missing file is expected.
- Go functions usually return errors.
- Do not choose panic.
Show Solution
Go code
choice := "returned error"
fmt.Println(choice)Line-by-line explanation
- The string states the normal mechanism.
- Println displays it.
Expected output
returned errorThis is the expected result.