liveBook · Manning

Default case

As explained in Learn Concurrent Programming with Go

A default case is the select branch that executes immediately when no other channel communication case is ready.

A default case makes a select statement non-blocking. When no channel operation is ready, its instructions run immediately, allowing the current execution to do other work instead of waiting. section 8.1.2

Polling for a delayed message

A loop can repeatedly try to receive a message while the default case reports that nothing is waiting. After the delayed sender transmits "Hello", the receive case runs and the program returns. Adding a short sleep prevents the loop from repeatedly printing without pause. section 8.1.2

The same pattern can coordinate concurrent work:

  • Place computation under default so a goroutine continues processing when no stop signal is available. section 8.1.3
  • Use a shared stop channel as the other case; when one goroutine closes it, the remaining goroutines detect the signal and return. section 8.1.3

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains selection behavior, demonstrates polling, and applies it to cancellable goroutines; it stops short of implementation internals.
Examples
Uses a delayed message loop and a parallel password search stopped through a closed channel.
What it assumes
Assumes familiarity with Go channels, select statements, and goroutines while demonstrating their coordination through small examples.
What you can do after
Apply it via Go's select statement to poll channels or keep workers responsive to cancellation.
How it connects
Connects select statements with channels, goroutines, channel closing, message passing, and timer channels.

Also explained in

Compare how 2 books explain default case →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.