Golang / GoLang Production Patterns and Web Standards Interview Questions
How do you wrap errors in Go 1.13+ and use errors.Is and errors.As for inspection?
Go 1.13 introduced the %w verb in fmt.Errorf and the errors.Is / errors.As functions to create and inspect error chains. Wrapping preserves the original error while adding context — callers can still check for specific error types or sentinel values anywhere in the chain.
import "errors"
// Sentinel errors — comparable with errors.Is
var (
ErrNotFound = errors.New("not found")
ErrPermission = errors.New("permission denied")
)
// Wrapping with %w — preserves the error chain
func fetchUser(id int) (*User, error) {
u, err := db.QueryUser(id)
if err != nil {
return nil, fmt.Errorf("fetchUser(id=%d): %w", id, err)
}
return u, nil
}
// errors.Is — checks the entire chain for a target value
err := fetchUser(42)
if errors.Is(err, ErrNotFound) {
http.Error(w, "user not found", http.StatusNotFound)
return
}
// Custom structured error type
type ValidationError struct {
Field string
Message string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("validation error on %s: %s", e.Field, e.Message)
}
func validateAge(age int) error {
if age < 0 {
return fmt.Errorf("validateAge: %w",
&ValidationError{Field: "age", Message: "must be non-negative"})
}
return nil
}
// errors.As — extracts a specific error type from the chain
var ve *ValidationError
if errors.As(err, &ve) {
http.Error(w,
fmt.Sprintf("invalid field %s: %s", ve.Field, ve.Message),
http.StatusBadRequest)
}
// errors.Unwrap — one level of unwrapping
inner := errors.Unwrap(err) // returns the wrapped error
// Go 1.20+: errors.Join — wrap multiple errors in one
combined := errors.Join(err1, err2)
errors.Is(combined, err1) // trueKey difference: %v formats the error as a plain string — the wrapped error is lost for inspection purposes. %w both formats it AND preserves the wrapped error so errors.Is and errors.As can traverse the chain.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
