02 / 10

How does the select statement work with multiple channels, including a default case?

select blocks until one of its cases can proceed, choosing randomly when multiple are ready. A default case makes it non-blocking, executing immediately if no channel is ready.

select patterns
Important behaviors
  1. 1

    When multiple cases are ready, select picks one uniformly at random — no priority ordering

  2. 2

    Nil channels are never ready — use this to dynamically disable a case

  3. 3

    time.After creates a new timer and channel on every call — use time.NewTimer in loops to avoid leaks

  4. 4

    default makes select non-blocking — runs immediately if no channel is ready

  5. 5

    select with only a default case and no channels is a spin loop — avoid