Middle · Go foundations

Panic, recover and failure boundaries

Can a parent goroutine recover a child goroutine's panic?

Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.

Reveal a strong answer
  1. recover only stops a panic when called directly by a deferred function on the panicking goroutine's stack. A parent goroutine's deferred recovery does not catch an unrelated child panic.

  2. Use errors for expected failures such as invalid input or unavailable storage. Panic usually represents a violated invariant or programming error; recovery at a server boundary can protect the process but cannot prove shared state remains valid.

  3. Cleanup still needs ownership and error reporting. Do not convert every panic into a successful zero result, and do not use panic/recover as ordinary cross-goroutine control flow.

Common trap

A defer in main is not a global panic handler for every goroutine.

Try it

A worker panics after updating half a shared structure. Is recovering and retrying automatically safe?

Worked solution

No. Recovery prevents stack unwinding from ending the process only within its valid boundary; it does not roll back partial mutations. Restore or discard inconsistent state according to the invariant, and report the failure. Prefer atomic state transitions and test the failure boundary.

Change the constraint

Which errors should an HTTP recovery middleware hide from a public response while retaining diagnostic evidence?

How did you do?

Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.

Not marked. Progress stays on this browser, separate from course completion.

Primary references

Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.