去<陈开销

I have been trying to understand channels in Go for a while now. But one thing makes me wonder. What happens exactly when you call,

For {
  select {
    case <-chan:
  }
}

Does it check the chan every for iteration for updates? How would it compare to a normal case i = int:, or a case atomic.LoadUint64() = uint64: performance-wise?

The Go Programming Language Specification

Select statements

Execution of a "select" statement proceeds in several steps:

  1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
  5. The statement list of the selected case is executed.

Go select statement behavior is defined in The Go Programming Language Specification. <-chan is evaluated once.

For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement.

The implementation of channels is not defined by the language specification (and will never be), so a particular implementation is free to do anythng it wants while it conforms to the specification.

On the other hand, accessing a channel behaves like accessing a mutex so I beleive internally Go arranges to use system-provided means of blocking on an appropriate resource. Think of WaitForMultipleObjects() on Windows, futexes on Linux and so on.

In other words, while the for { select { case <-chan: ... } } construct looks like busy waiting on the objects in those case labels, in fact it is not: the compiler leverages the system's means of making the Go runtime scheduler be notified when some of those resources becomes available.