liveBook · Manning

Concurrency

As explained in Go in Practice, Second Edition

Concurrent execution is the progression of multiple computations during overlapping time intervals.

Concurrency lets a program progress through multiple independent tasks during overlapping time intervals. Go favors goroutines and channels over a traditional threading model, keeping concurrent work practical and relatively inexpensive to manage. section 5.1 section 5.0

A practical Go implementation typically combines these pieces:

  • Start a function as a goroutine with the go keyword; the caller continues immediately while the function runs independently. section 5.1 section 5.2
  • Use channels as typed pipelines for sending data or signals between goroutines; unbuffered channels block by default and provide synchronization. section 5.1 section 1.2.3
  • Account for application lifetime: when main returns, the program ends and active goroutines stop, so explicit waiting or coordination is required. section 5.2
  • Prefer context deadlines and timeouts for production-style cancellation instead of hand-built sleeping timers. section 5.2

Concurrent counting and output

A count function can print 0 through 4 while main prints Hello World. Calling go count() allows both functions to proceed concurrently; time.Sleep makes the demonstration's ordering visible. A channel version sends 8, 6, 7, 5, 3, 0, 9, and -1 to a receiving goroutine, which stops after the negative value.

How Go in Practice, Second Edition covers it

How deep it goes
Explains goroutines, channels, blocking, direction, and lifetime with runnable examples, while deferring implementation theory and CSP underpinnings.
Examples
Uses concurrent printing, integer channel transfer, and a thirty-second terminal echo task.
What it assumes
Assumes basic Go functions and program flow while introducing goroutines, channels, and Go's concurrency model practically.
What you can do after
Apply it via Go goroutines, channels, sleeps, and context deadlines to coordinate concurrent tasks and communication.
How it connects
Connects goroutines with channels, mutexes, CSP, context deadlines, timeouts, and synchronization.

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 Practice, Second Edition. The section links point to the parts of the book it draws on.