When Go's Race Detector Passes but an Update Is Lost

A balance starts at 100. One worker adds 10, another subtracts 20. When both updates have finished, the balance should be 90. But a Go program can print 80, even when go test -race reports no data race. The detector checked memory accesses; it didn’t check whether the whole balance update was correct. The experiment below forces the problematic ordering instead of hoping that a fast machine triggers it. It uses Go 1.27.x on Linux/amd64; the relevant atomic semantics come from Go’s memory model rather than that particular machine. ...

October 30, 2026 · 5 min · 995 words · Jack Li

Why a Closed Go Channel Is Still Ready to Receive

Imagine a collector listening to two channel inputs. One producer finishes, closes its channel, and never sends again. You expect the collector to ignore that input from now on. Instead, its select keeps entering the closed channel’s receive case. Why would an input with no remaining values still be ready? The distinction depends on whether the channel is open and empty, closed, or referenced through nil. A closed channel can still complete a receive Closing a channel does not discard values already in its buffer. After those values have been received, future receives complete immediately with the element type’s zero value and ok == false. ...

October 17, 2026 · 4 min · 759 words · Jack Li

Go select Is a One-Shot Choice, Not a Continuous Listener

It is easy to look at a Go select and imagine a small event loop that keeps watching every channel forever. That mental model is close enough for simple code. It becomes misleading when a case body changes channel state or another goroutine becomes runnable. The smaller model is more useful: one select execution → choose one communication that can proceed → perform that communication → run that case body → this select execution is finished If the program should keep watching channels, something outside that select must enter it again. Most often, that is a for loop. ...

September 22, 2026 · 6 min · 1207 words · Jack Li