liveBook · Manning

Failure

As explained in Go in Action, Second Edition

An error is a value or condition indicating that an operation failed or cannot produce its intended result.

Failure appears as an error value or condition when an operation cannot produce its intended result. Go favors direct, explicit handling over exception-based control flow, making the failure path part of ordinary program logic. section 7.0 section 7.4

A function typically returns an error as its last result. Any type implementing Error() string can represent one, allowing errors to carry either a message or structured diagnostic data. section 7.13 section 7.9

Handle failures deliberately:

  • Check the error as soon as the function returns it rather than silently discarding it. section 7.13 section 7.4
  • Log useful context, then return or wrap the error so callers can continue the decision path. section 7.4 section 7.13
  • Use errors.Is() for a particular error and errors.As() for a particular error type. section 7.13
  • Reserve panic for truly unexpected conditions; use ordinary errors for expected operational failures. section 7.13 section 7.4

Reporting missing keys in a bulk operation

A key/value store can define a bulkGetError slice containing every missing key. Its Error() method formats those keys, while BulkGet continues collecting successful values, returns the custom error when any key is absent, and wraps unexpected failures. section 7.9

Listing 7.6 An example of not capturing an error
val, _ := store.Get("fizz")

Shows the compact form of discarding a returned error, useful for contrasting brevity with deliberate failure handling. section 7.4

How Go in Action, Second Edition covers it

How deep it goes
Explains error values, interfaces, propagation, wrapping, custom types, inspection, logging, and panic boundaries.
Examples
Demonstrates ignored errors, key/value lookups, bulk retrieval, missing-key aggregation, logging, and wrapped unexpected failures.
What it assumes
Assumes working knowledge of Go functions and interfaces while defining the error interface and explicit handling model.
What you can do after
Diagnose/debug with it by checking, logging, propagating, wrapping, and type-inspecting failures in Go code.
How it connects
Connects failures to error interfaces, error wrapping, sentinel errors, logging, panic, recover, and key/value stores.

Also explained in

Compare how 12 books explain failure →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.