Go Reference ยท Operators
The <- channel operator
Reference for Go's <- operator: sending to and receiving from channels, the comma-ok receive form, blocking rules, nil and closed channels, and direction types.
The arrow <- is how goroutines talk through channels. As a binary operator, ch <- v sends the value v into the channel ch. As a unary operator, <-ch receives a value from ch and yields it as an expression. In channel types, the arrow also marks direction: chan<- T can only send, <-chan T can only receive.
Sends and receives on an unbuffered channel block until the other side is ready, which makes channels a synchronization tool as well as a data pipe.
ch <- 42 // send v := <-ch // receive v, ok := <-ch // receive, ok is false if closed and drained
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
ch <- value // send statement x := <-ch // receive expression x, ok := <-ch // receive with ok <-ch // receive and discard chan<- T // send-only channel type <-chan T // receive-only channel type
| Name | Description |
|---|---|
ch |
A channel value. A nil channel blocks forever on both send and receive. |
value |
Must be assignable to the channel's element type. |
ok |
true if the value was sent before the channel was closed; false when the channel is closed and empty (and x is the zero value). |
Blocking and failure rules
- Unbuffered channel: a send waits for a receiver, and a receive waits for a sender.
- Buffered channel: a send blocks only when the buffer is full; a receive blocks only when it is empty.
- Sending on a closed channel panics. Receiving from a closed channel never blocks: it drains remaining values, then returns zero values.
- Closing a nil channel or an already closed channel panics.
- If every goroutine is blocked, the runtime reports "fatal error: all goroutines are asleep - deadlock!".
Usage notes
<-associates with the leftmostchanit can:chan<- chan intis a send-only channel of channels. Add parentheses when in doubt.- A bidirectional
chan Tconverts implicitly tochan<- Tor<-chan T, never the other way. Use directional types in function signatures to document who sends and who receives. - Only the sender should close a channel, and only when receivers need to know no more values are coming. Receivers can detect that with
okorfor v := range ch. - Use
selectto wait on several channels, add timeouts, or avoid blocking; see select. <-chas a statement discards the value, commonly used to wait for achan struct{}signal or<-ctx.Done().- Channel operations are safe for concurrent use, with no extra locking.
- The value sent is copied. Sending a pointer shares the pointed-to data, so two goroutines can still race on it.
- A send or receive in the middle of an expression is evaluated in the normal order, but avoid clever one-liners:
a[<-ch] = <-chis legal and hard to read.