Go Reference ยท Keywords and Statements
The chan type
Reference for Go channel types: chan T, send-only and receive-only directions, buffered and unbuffered behavior, closing, nil channels and the comma-ok receive.
A channel is a typed conduit through which goroutines send and receive values. chan T is the type of a channel carrying values of type T. Channels both transfer data and synchronize: an unbuffered send waits until another goroutine receives, so the two meet at that moment.
You create channels with make. The zero value of a channel type is nil. Channels can be restricted to a single direction in a type, which documents intent and lets the compiler catch mistakes.
ch := make(chan int) // unbuffered buf := make(chan string, 8) // buffered, capacity 8 ch <- 42 // send v := <-ch // receive
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
ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType .
| Type | Meaning |
|---|---|
chan T |
Bidirectional: can send and receive. |
chan<- T |
Send-only. |
<-chan T |
Receive-only. |
Operations: ch <- v sends, <-ch receives, v, ok := <-ch receives and reports whether the value came from a send (ok is false when the channel is closed and empty), close(ch), len(ch), cap(ch).
Usage notes
- Blocking rules. An unbuffered send blocks until a receiver is ready. A buffered send blocks only when the buffer is full; a receive blocks when the buffer is empty.
- Closed channels. Receiving from a closed channel returns remaining buffered values, then the zero value immediately (
ok == false). Sending to a closed channel panics, and closing a closed or nil channel panics. - Who closes. Only the sending side should close, and only when receivers need to know that no more values are coming. Closing is not required to free the channel.
- Nil channels. Sending or receiving on a nil channel blocks forever. In a
select, that makes the case never ready, which is a useful way to disable a case. - Direction conversion. A bidirectional channel converts implicitly to
chan<- Tor<-chan T, but not the other way around. - Deadlocks. If every goroutine is blocked, the runtime ends the program with "all goroutines are asleep - deadlock!". Blocked goroutines that others could still wake do not trigger this.
- Element types. Channels of channels (
chan chan int) and ofstruct{}(signals only) are common. - Choosing. Channels fit ownership transfer and pipelines; a
sync.Mutexfits guarding shared state. - Comparison. Channels are comparable with
==; two channel values are equal if they came from the samemake.
Related
go,select,close(),make(),cap(), Channels- Lessons: Channels, Buffered channels and select, Concurrency patterns