Contents

Golang Note Five

Select

  • The select statement lets a goroutine wait on multiple communication operations.
  • A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

Default Selection

  • The default case in a select is run if no other case is ready.
  • Use a default case to try a send or receive without blocking:
1
2
3
4
5
6
select {
case i := <-c:
    // use i
default:
    // receiving from c would block
}