liveBook · Manning

Chapter 9: Concurrency

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

The chapter introduces the difference between concurrency and parallelism, explains why concurrency helps applications stay responsive, and previews Go’s tools for concurrent work.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces the difference between concurrency and parallelism, explains why concurrency helps applications stay responsive, and previews Go’s tools for concurrent work.

Key terms: concurrency, parallelism, responsive, goroutines, channels, Go runtime, scheduling

9.1 When to use concurrency

This section uses an RSS reader to show when concurrency helps, how the go keyword starts concurrent work, and why that work needs reliable synchronization.

Key terms: concurrency, independent tasks, blocking I/O, parallelism, RSS feed, synchronous operation, gofeed, blocked, go keyword, goroutine

9.2 Sync package

Introduces the sync package as a way to coordinate goroutines and protect shared resources.

Key terms: goroutines, sync package, shared resources, synchronize, coordinate the completion of tasks

9.2.1 Wait groups

This section shows how a sync.WaitGroup reliably waits for concurrent feed-fetching goroutines to finish, demonstrates two ways to track them, and notes that a WaitGroup does not collect errors.

Key terms: WaitGroup, counter, goroutines, synchronization primitive, WaitGroup.Go, Add, defer wg.Done(), anonymous functions, error handling

9.2.2 Error Groups

This section shows how errgroup waits for concurrent tasks and reports their errors.

Key terms: errgroup.Group, WaitGroup, Go, Wait, non-nil error

9.2.3 Mutexes

This section explains how mutexes protect a shared article store from races while allowing concurrent reads when no write is in progress.

Key terms: race condition, mutex, critical section, mutual exclusion, RWMutex, Lock, Unlock, RLock, RUnlock, ArticleStore

9.3 Adding some context

The section adds contexts to feed fetching so downstream HTTP operations can time out and related fetches can be canceled together.

Key terms: context, timeout, cancel function, context package, Context interface, metadata, downstream, temporary values, derived context, error group

9.4 Channels

This section introduces channels as a way for goroutines to pass values and coordinate work, then applies them to a feed-fetching application with buffering, error handling, and select.

Key terms: communicating sequential processes (CSP), channels, goroutines, <- operator, make, channel, producer, consumer, range, close

9.5 Concurrency patterns

This section introduces worker pools and fan-out/fan-in as patterns for improving throughput while keeping concurrent work coordinated.

Key terms: worker pool, worker goroutines, shared queue, channel, parallelism, fan-out, fan-in, goroutines, results

9.5.1 Workers

This section builds a background feed-ingestion worker pool that uses a shared channel to distribute work among concurrent workers.

Key terms: worker, channel, queue, App struct, WorkerPool, buffered channel, shared queue, pool size, run loop, worker goroutines

9.5.2 Fan-out and fan-in

This section uses fan-out/fan-in to refresh multiple feeds concurrently and return their combined articles and errors in one response.

Key terms: fan-out, goroutine, results channel, fan-in, WaitGroup, for-range loop, fetchFeeds, handler, articles, errors

9.6 Helpful concurrency tips

This introduction frames Go concurrency as useful but potentially risky and previews guidance on safe practices and when to use it.

Key terms: goroutines, channels, race conditions, deadlocks, resource leaks, best practices

9.6.1 Best practices for Go concurrency

This section gives practical guidelines for writing clear, safe, and manageable concurrent Go code.

Key terms: channels, shared memory, sync.Mutex, sync.RWMutex, race conditions, context.Context, cancellation, deadlines, request-scoped values, close channels

9.6.2 When to use concurrency in Go

This section identifies situations where independent or parallel work can make Go applications faster or more responsive.

Key terms: concurrency, independently, in parallel, responsiveness, goroutine, I/O-bound operations, background tasks, throughput, responsive user interfaces, data pipelines

9.6.3 When not to use concurrency in Go

This section explains when Go concurrency is a poor fit and recommends starting with a sequential solution before adding it where needed.

Key terms: sequential approach, synchronization, race conditions, goroutines, channels, sequential solution

9.7 Summary

Summarizes Go’s concurrency tools, common patterns, and when concurrency is worth using.

Where this is used later

Concepts explained in this chapter

← Chapter 8: Testing and tooling Chapter 10: The standard library →

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.