liveBook · Manning

Chapter 7: Errors in Go

What you will learn in chapter 7 of Go in Action, Second Edition

The introduction explains why software must handle unexpected conditions and previews Go’s explicit, flexible approach to errors through a small key/value database example.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why software must handle unexpected conditions and previews Go’s explicit, flexible approach to errors through a small key/value database example.

Key terms: unexpected conditions, error handling, directness, explicitness, exception handling, error type, custom error types, panic, recover, key/value database

7.1 Go errors are values

This section introduces Go’s practice of returning errors as values and checking them immediately where they occur.

Key terms: error type, error value, return values, if err != nil, nil, error-handling flow, if statement

7.2 The Go error type

This section defines Go’s minimal error interface and emphasizes handling errors as soon as a function returns them.

Key terms: error, interface, Error(), nil, error check, annotating, handle your results as soon as you get them

7.3 Generating new errors

This section compares two ways to create Go errors and shows how placeholder errors can support an interface before its implementation is ready.

Key terms: errors.New, fmt.Errorf, static string, formatted errors, Store interface, placeholders

7.4 Handling errors

This section explains why Go errors call for an explicit decision where they occur, how errors can be logged and passed to callers, and how an application may ultimately exit.

Key terms: errors as values, try/catch, antipattern, log, calling function, wrapping an error, os.Exit(int), panic(any), panic(nil)

7.5 Logging errors

This section shows how to report errors with Go’s fmt and log packages and how log adds configurable information to messages.

Key terms: fmt, log, standard library, timestamp, flags, log.SetFlags(0)

7.6 Annotating error logs with formatting

The section shows how to make ambiguous Go errors more useful by adding context when logging them with formatted text.

Key terms: annotating errors, context, %s, %v, Error(), structured logging, slog

7.7 Sentinel errors

This section introduces sentinel errors as stable, recognizable error values for common conditions and shows why they help error-handling code.

Key terms: sentinel error, error value, identity, Err, package level, io.EOF

7.8 Identifying different types of errors

This section shows how to recognize sentinel errors with errors.Is, including when they are wrapped, so code can respond to specific failures.

Key terms: errors.Is(), sentinel errors, %w, wrap errors, error interface, errors are values, errors.As()

7.9 Creating a custom error type

This section builds a custom Go error that records missing keys during a bulk lookup and explains why an error type can carry more than a message.

Key terms: custom error type, error interface, Error() string, slice of strings, bulk get, sentinel error, missing keys, nil

7.10 Getting the underlying error with errors.As

This section shows how to use errors.As to recover a specific underlying error type and act on its task-specific data.

Key terms: errors.As, error interface, concrete type, interface, pointer to a pointer, BulkGet, keys, placeholders, godoc, exported error type

7.11 An unexpected error journey

This section explains when a Go program may exit through a panic and uses an uninitialized key-value store to show how an explicit panic can give a clearer message than a nil-reference failure.

Key terms: panic, nil reference, panic function, external service, clustered systems, self-healing, KVStore, NewKVStore, Keys, uninitialized kv store

7.12 Don’t panic

This section explains when panic recovery is worth attempting and introduces a deferred recover call that lets a wrapper handle a panic from KVStore.Keys().

Key terms: defer, recover, panic(nil), SafeKeys, KVStore, Keys(), panic, recovery, uptime, durability

7.12.1 Using Panic and Recover in your code

This subsection explains why Go code should usually return errors instead of panicking, when a panic might be justified, and why recover is usually a temporary workaround.

Key terms: panic, error, inconsistent state, godoc, recover

7.13 Summary

The chapter summary recaps how Go errors are represented, handled and identified, and why panics are not a substitute for ordinary errors.

Where this is used later

Concepts explained in this chapter

← Chapter 6: Generics Chapter 8: Testing and tooling →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.