liveBook · Manning

Concurrency

As explained in Go in Action, Second Edition

Concurrent programming is the design of software whose multiple execution activities can make progress during overlapping periods of time.

Concurrency lets a program make progress on multiple tasks without finishing one before starting another. It is not the same as parallelism: tasks can be managed concurrently even when they are not executing at the same instant. In Go, this is useful when independent work waits on network or disk I/O, or when background work must not block a responsive application. section 9.0 section 9.1

Start with a sequential solution, then add concurrency where independent tasks justify its complexity. A goroutine runs a function concurrently; channels pass typed values between goroutines and coordinate their work. This message-passing approach can reduce the need to synchronize shared memory by hand. If goroutines do share data, protect it with a mutex. section 9.1 section 9.4 section 9.6.1

Fetching RSS feeds

An RSS reader can fetch a New York Times feed and a BBC feed in separate goroutines instead of waiting for each request in turn. A `gofeed` parser retrieves each feed; the fetch stage then sends its articles through a channel to a storage stage. A receiving stage can use `for article := range articles` to process values until the sending stage closes the channel. The application can expose saved articles through an HTTP handler. section 9.1 section 9.4

Give every concurrent task a way to finish and every failure a way to surface:

  • Track completion with `WaitGroup` or `errgroup`, rather than relying on a plain `go` statement. section 9.6.1
  • Pass `context.Context` for cancellation and deadlines, and close channels when their work is done. section 1.4.2 section 9.6.1
  • Limit active goroutines with a worker pool or an `errgroup` limit; propagate errors and run `go test -race` while developing. section 9.6.1
  • Prefer sequential code for a single task, strictly ordered operations, or work too small to repay coordination overhead. section 9.6.3

How Go in Action, Second Edition covers it

How deep it goes
Works through goroutines, channel-based processing, and operational safeguards, but does not descend into runtime scheduler internals.
Examples
Builds an RSS reader that fetches news feeds concurrently, passes articles to storage, and exposes saved articles over HTTP.
What it assumes
Assumes a straightforward sequential Go program as a starting point and introduces goroutines and channels through its concurrent revision.
What you can do after
Apply it via `gofeed` to fetch multiple RSS feeds with goroutines and route articles through channels for storage.
How it connects
Connects goroutines and channels to context cancellation, mutexes, worker pools, pipelines, and race conditions.

Also explained in

Compare how 23 books explain concurrency →

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.