Go Developer interview question

How do you design error handling in Go?

In Go, errors are explicit return values. Add context with fmt.Errorf and %w, preserve the cause for errors.Is or errors.As, and decide at the boundary whether the caller can recover.

Direct answer

What a strong answer covers

How it works

Errors are values, so the design questions are the same as for any value: what the caller needs to decide, how much context each layer adds, and where the error stops. Wrap with %w to keep the cause inspectable, use sentinel errors or typed errors only where a caller branches on them, and translate to the transport format at the boundary. Logging and returning the same error produces duplicate noise, so pick one per layer.

Example

Return fmt.Errorf("load profile: %w", err) so a handler can still recognise a sentinel error and choose the correct HTTP status. The handler maps errors.Is(err, ErrNotFound) to 404, logs everything else once with the request ID, and returns a generic 500 body so internal details never reach the client.

Common mistakes

  • Wrapping with %v and losing the ability to use errors.Is and errors.As.
  • Checking error strings with strings.Contains instead of typed comparisons.
  • Exposing raw database or provider error text in API responses.

A useful answer structure

Answer from your own work.

Do not memorise a script: use only examples you can explain honestly in follow-up questions.

Practise this answer with MiraHint

Open the desktop workspace and rehearse a clear answer in your own words.

Download MiraHint
Go Developer interview questions