<-
. This allows go-routines to synchronize without explicit lock or condition variables.ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and
// assign value to v.
make
to initialize a buffered channel: ch := make(chan int, 100)
.close
a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression: after v, ok := <-ch
; ok
is false
if there are no more values to receive and the channel is closed.for i := range c
receives values from the channel repeatedly until it is closed.select
statement lets a go-routine 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.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: