Deep Dive into the Select Statement
summary
Compiler lexing/parsing/typecheck turns select into IR/SSA and special-cases it: zero-case becomes a call to runtime.block, single-case and single-plus-default emit nonblocking helpers (selectnbrecv/selectnbsend), and multi-case delegates to runtime.selectgo. runtime.selectgo shuffles cases with cheaprand, locks channels in address-sorted order to avoid deadlocks, checks readiness, subscribes the goroutine to all channels’ wait lists, and desubscribes on wake; selection is randomized for fairness and there is no built-in priority. Version targeted is Go 1.26.5.
AI-written summary · about
chapters (11)
- 0:00Introduction
- 1:50Select statement in Go
- 6:05Compiler’s role
- 11:25IR optimizations for select
- 14:18Runtime execution
- 18:33SelectMB send/receive
- 21:25Select function in runtime
- 29:48Compiler’s final handling
- 31:13Key characteristics
- 32:59References
- 33:41Q&A
related talks
21mHandling the Selection Monad
07mRace Conditions in Details
01h 17mSmart Handlers: Handling the selection monad
43mUnderstanding the Go Compiler
26mConcurrency primitives of Golang - how to use and how not to
27mProduction Ready Concurrency
47mQueues, Fairness, and The Go Scheduler
22mData race detection
