Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Types

Go channels

Reference for Go channels: unbuffered and buffered channels, send and receive, closing, range over a channel, directions and the rules for nil channels.

A channel is a typed conduit through which goroutines send and receive values. The type chan T carries values of type T. Channels both pass data and synchronize: an unbuffered send waits until a receiver is ready, and the receive waits until a sender arrives.

You create channels with make. A buffered channel has room for a fixed number of values, so sends only block when it is full. The zero value of a channel is nil.

Go
ch := make(chan string)
go func() { ch <- "ping" }()
fmt.Println(<-ch)

Try it

Go Unbuffered, buffered and close

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 Directions and a pipeline

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
var c chan T            // bidirectional, nil
c = make(chan T)        // unbuffered
c = make(chan T, n)     // buffer of n values
var s chan<- T          // send-only
var r <-chan T          // receive-only
c <- v                  // send
v := <-c                // receive
v, ok := <-c            // ok is false if closed and drained
close(c)
Operation Behavior
Send on nil channel Blocks forever.
Send on closed channel Panics.
Receive from nil channel Blocks forever.
Receive from closed channel Returns buffered values first, then the zero value with ok == false.
close(c) Panics if c is nil or already closed.
len(c), cap(c) Number of queued values and buffer size.

Usage notes

  • Only the sender should close. Closing signals "no more values"; receivers cannot know whether it is safe to close. Never close a channel just to clean up if the garbage collector can do it: a channel does not need to be closed to be freed.
  • A channel value implicitly converts from chan T to <-chan T or chan<- T, never the other way. Use directions in function signatures to document intent.
  • Channels are comparable and can be used as map keys; equality means the same channel.
  • select lets you wait on several channel operations, add a default, or combine with timeouts. See select.
  • Deadlocks. If every goroutine is blocked, the runtime aborts with "all goroutines are asleep - deadlock!". Blocking forever in only some goroutines is a leak instead (see Goroutine leaks).
  • A buffer is not a queue for throughput tuning. Use a size you can justify (for example the number of workers); larger buffers can hide bugs.
  • A chan struct{} carries no data and is the idiomatic signal channel. Closing it wakes every waiter at once.
  • Mutexes are often simpler when you only protect shared state.