Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The <- token, known as the channel operator, dictates the flow of data into and out of channels in Go. It functions as a syntactic token within send statements and as a unary prefix operator in receive expressions. The position of the arrow relative to the channel variable visually represents the direction of value transfer, enforcing synchronization and safe data exchange between goroutines.

Send Operations

In a send statement, the <- token separates the target channel on the left from the value to be sent on the right. Semantically, Go evaluates both the channel expression and the value expression before the actual communication begins. Once both are evaluated, the resulting value is written to the channel.
// Syntax: ChannelExpression <- ValueExpression
ch <- 42

// Both getChannel() and getValue() are fully evaluated before the send operation blocks or executes
getChannel() <- getValue()

Receive Operations

When positioned to the left of a channel expression, <- acts as a unary prefix operator. It reads and dequeues a value from the channel. The resulting value can be assigned to a variable, evaluated within a larger expression, or discarded.
// Assignment
v = <-ch

// Initialization
v := <-ch

// Discard (evaluated for synchronization, value is dropped)
<-ch
The Comma-Ok Idiom The receive operation can optionally yield a second untyped boolean value in an assignment. This boolean indicates whether the received value was successfully sent by a goroutine (true) or if it is a zero value generated because the channel is closed and empty (false).
v, ok := <-ch

Type Declarations (Directionality)

Beyond executable statements, the <- token is used in type signatures to constrain channel directionality at compile time.
var ch chan int       // Bidirectional: supports send, receive, close(), len(), and cap()
var chSend chan<- int // Send-only: supports send, close(), len(), and cap()
var chRecv <-chan int // Receive-only: supports receive, len(), and cap()
Note: The <- operator associates with the leftmost chan keyword possible. For example, chan<- chan int is parsed as chan<- (chan int) (a send-only channel of bidirectional channels).

Operational Mechanics and State Behavior

The behavior of the <- operator is strictly governed by the initialization state, buffering, and closure status of the target channel. Unbuffered Channels
  • Send (ch <- v): Blocks the executing goroutine until another goroutine invokes a receive operation on the same channel.
  • Receive (<-ch): Blocks the executing goroutine until another goroutine invokes a send operation on the same channel.
Buffered Channels
  • Send (ch <- v): Blocks only if the channel’s underlying buffer is at maximum capacity.
  • Receive (<-ch): Blocks only if the channel’s underlying buffer is completely empty.
Nil Channels
  • Send (ch <- v): Blocks indefinitely.
  • Receive (<-ch): Blocks indefinitely.
Closed Channels
  • Send (ch <- v): Triggers a runtime panic: send on closed channel.
  • Receive (<-ch): Never blocks. It immediately returns the remaining buffered values. Once the buffer is empty, it continuously returns the zero value of the channel’s element type.
Master Go with Deep Grasping Methodology!Learn More