Go treats errors as values implementing error—not exceptions like Java. Functions return (T, error); callers check explicitly.
Creating and wrapping errors
if err != nil {
return fmt.Errorf("read config: %w", err)
}
errors.Is and errors.As inspect error chains. panic is for programmer bugs, not expected failures.
Custom errors
Types implementing Error() string can carry extra fields. Keep error messages actionable for operators and logs.
Important interview questions and answers
- Q: Exceptions in Go?
A: No—use explicit error returns; panic/recover only for truly exceptional paths. - Q: %w in fmt.Errorf?
A: Wraps errors forerrors.Is/Aschain inspection.
Self-check
- What interface do errors implement?
- When is panic appropriate?
Tip: Wrap with %w and inspect with errors.Is/errors.As—preserve error chains for logging and API mapping.
Interview prep
- Exceptions in Go?
No—return error values; panic for programmer bugs, not expected failures.
- %w in fmt.Errorf?
Wraps errors for errors.Is and errors.As chain inspection.