liveBook · Manning

Failure

As explained in 100 Go Mistakes and How to Avoid Them

An error value is a value representing an error condition that can be compared, returned, or propagated by a Go program.

In Go, a failure is represented by an error value returned as an ordinary result, rather than handled through try/catch. The value can be compared, returned, wrapped with context, logged, or propagated. section 7.0 section 7.8

Avoid these common mistakes when handling failures:

  • Do not compare a possibly wrapped error directly with a sentinel using ==; use errors.Is instead. section 7.4 section 7.8
  • Do not silently discard an error from a deferred operation. Handle it, propagate it, or ignore it explicitly with the blank identifier. section 7.7 section 7.8
  • Log or return an error according to the context; logging already counts as handling it. section 7.8

Checking an expected failure

A query may return sql.ErrNoRows when no rows are found. Because this expected condition is a sentinel error, compare it with errors.Is(err, sql.ErrNoRows), especially when another function may have wrapped it with additional context. section 7.4 section 7.8

const query = "..."
 
func getBalance(db *sql.DB, clientID string) (
    float32, error) {
    rows, err := db.Query(query, clientID)
    if err != nil {
        return 0, err
    }
    defer rows.Close()     #1
 
    // Use rows
}

Inspect the deferred database-row cleanup example, then compare explicit ignoring, logging, and propagation of the Close error. section 7.7

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains ordinary returns, sentinel values, error types, wrapping, comparison, and deferred cleanup, but not package internals.
Examples
Uses sql.ErrNoRows, io.EOF, database row cleanup, and deferred Close errors to expose comparison and handling choices.
What it assumes
Assumes familiarity with Go functions and return values while teaching sentinel errors, error types, wrapping, and deferred cleanup.
What you can do after
Diagnose error-handling mistakes and choose whether to compare, wrap, log, return, propagate, or explicitly ignore failures.
How it connects
Connects error values with sentinel errors, error types, error wrapping, deferred functions, logging, and database operations.

Also explained in

Compare how 12 books explain failure →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.