Skip to content
Tutorials Center
My learning

Topics

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.

Go
ch <- 42      // send
v := <-ch     // receive
v, ok := <-ch // receive, ok is false if closed and drained

Try it

Go Send and receive between goroutines

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.

Go Buffers, direction and select

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

Go
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 leftmost chan it can: chan<- chan int is a send-only channel of channels. Add parentheses when in doubt.
  • A bidirectional chan T converts implicitly to chan<- T or <-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 ok or for v := range ch.
  • Use select to wait on several channels, add timeouts, or avoid blocking; see select.
  • <-ch as a statement discards the value, commonly used to wait for a chan 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] = <-ch is legal and hard to read.