Go 的併發模型基於 CSP(Communicating Sequential Processes):透過共常式(Coroutine,在 Go 中稱為 Goroutine)與通道(Channel)達成「以通訊代替共享」。
設計哲學#
Don’t communicate by sharing memory; share memory by communicating.
寫得好的 Go 併發程式幾乎只用 channel 與 goroutine。sync、atomic 是補位用的低階工具,等到 channel 表達不了或會造成大量複製時才退而求其次。