Send-only and receive-only channels are directional channel types in Go. The type chan<- T can only send, the type <-chan T can only receive, and plain chan T does both. They are Go directional channels: a compile time restriction that documents a function's role and stops it from misusing a channel.
A bidirectional channel converts to either direction implicitly, but never back. The restriction exists only in the type system, so there is no runtime cost.
The arrow position relative to the chan keyword gives the direction. Read it as data flowing along the arrow.
| Type | Meaning | Allowed operations |
|---|---|---|
chan T | Bidirectional | send, receive, close |
chan<- T | Send-only | send, close |
<-chan T | Receive-only | receive, range |
Trying a disallowed operation is a compile error. Sending on a receive-only channel reports invalid operation: cannot send to receive-only channel, and receiving from a send-only channel reports a similar message.
You almost always create a normal channel and pass it to functions that declare the direction they need.
data is created as chan int. Passing it to produce converts it to chan<- int, and passing it to consume converts it to <-chan int. Inside consume, the compiler would reject in <- 1, which prevents the bug of a consumer feeding its own input.
A common pattern is a function that starts a goroutine and returns the read end only, so callers cannot send or close.
Because the return type is <-chan int, callers cannot call close on it. The compiler gives invalid operation: cannot close receive-only channel. That keeps closing with the owner, in line with the rule that the sender closes.
close works on bidirectional and send-only channels, not on receive-only ones.[]chan<- int and []chan int are different types. You cannot assign one slice to the other, you must convert element by element.Pipelines chain stages where each reads from one channel and writes to the next. Direction types show the data flow in the signature.
Anyone reading the signature sees that square consumes one stream and produces another, with no hidden writes to in. This also helps a generics-based version, where you can write func Map[T, U any](in <-chan T, f func(T) U) <-chan U.
Ask what the function does with the channel. A worker that reads jobs takes <-chan Job. A worker that reports results takes chan<- Result. A function that both reads and writes the same channel, such as a router that forwards values back, needs chan T, and that is often a hint that responsibilities should be split.
No. They are the same channel at runtime. The restriction is checked only at compile time.
Go allows assigning a bidirectional channel value to a directional channel type when the element types are identical. The reverse is not allowed.
Yes for functions that only send or only receive. It documents intent and catches mistakes. Struct fields that you both send to and receive from usually stay bidirectional.
No. It is a compile error, which is intended, since the receiving side should not decide when the stream ends.
chan<- T is send-only, <-chan T is receive-only, and chan T is both. Create bidirectional channels, then pass or return the narrowest direction a function needs. The compiler blocks wrong operations, including closing a receive-only channel, and the cost at runtime is zero.