Go Web Frameworks interview question · Most asked
How do you design consistent error responses in a Go API?
Summary
Define one API error type (status, machine-readable code, message, optional field errors), one function that writes it, and map domain errors to it in one place; unexpected errors become a generic 500 while the real cause is logged.
Longer Answer
Use errors.Is/As to map sentinel and typed domain errors (ErrNotFound → 404, ErrConflict → 409). Echo's central HTTPErrorHandler is built for this; in other frameworks, write a helper or middleware. RFC 9457 (Problem Details) is a standard format to consider.
Key Points
- One error type and writer.
- Map domain errors centrally.
- Never leak internals.
Deep dive: Go Web Frameworks: Validation and Errors
More most-asked Go Web Frameworks questions
- How should a Go service use database/sql correctly?
- How do you implement graceful shutdown for a Go HTTP server?
- Compare Gin, Echo, Chi and Fiber.
- What is the http.Handler interface, and why is it central to Go web development?
- How do you test HTTP handlers in Go?
- How do you pass request-scoped values (like the current user) through middleware in Go?
- Do you need a web framework in Go? What does Go 1.22's ServeMux provide?