Concurrency

Channels

Send values between goroutines and understand blocking, buffering, closing, and ownership.

What you will learn

This lesson explains three related decisions: how a send meets a receive, when bounded buffering solves a real coordination problem, and which part of a program may safely announce that no further values will arrive.

A channel is a typed connection used by goroutines to send and receive values. Unlike a plain variable, it gives the two goroutines a defined rendezvous point: the sender cannot treat the hand-off as complete until the channel's buffering rules allow it, and the receiver cannot invent a value before one is available.

messages := make(chan string)

Its type is chan string: a channel carrying strings.

Send and receive

messages <- "ready" // send
message := <-messages // receive

For an unbuffered channel, a send waits until another goroutine is ready to receive, and a receive waits until another goroutine is ready to send. This transfers both a value and a synchronisation signal.

func main() {
    messages := make(chan string)

    go func() {
        messages <- "work complete"
    }()

    fmt.Println(<-messages)
}

Buffered channels

queue := make(chan string, 2)
queue <- "first"
queue <- "second"

A buffered channel lets sends proceed until the buffer is full. A receive waits only when the buffer is empty. The capacity therefore specifies how much work may be outstanding at this boundary; it is not a performance setting to choose by habit.

Buffers can absorb short bursts and limit the number of queued items. They do not remove the need to design backpressure or cancellation.

Closing a channel

Closing means no more values will be sent.

close(queue)

The component that can prove that no more sends will happen should close the channel. That is often the sending side or a coordinator that waits for all senders. A receiver normally cannot make that proof: another sender may still be about to write.

Sending to a closed channel panics. Closing an already-closed channel also panics.

Receive until closed

for value := range queue {
    fmt.Println(value)
}

The loop ends after the channel is closed and all buffered values have been received.

You can inspect closure directly:

value, ok := <-queue
if !ok {
    fmt.Println("channel is closed")
}

Directional channel types

Function signatures can communicate ownership:

func produce(out chan<- int) {
    out <- 42
}

func consume(in <-chan int) {
    fmt.Println(<-in)
}

chan<- int is send-only. <-chan int is receive-only.

Channels are not queues by default

An unbuffered channel has no storage. Even a buffered channel is usually best understood as a coordination mechanism with bounded storage, not a replacement for a durable job queue.

Common mistakes

Sending and receiving on the same unbuffered channel in one goroutine deadlocks, because neither operation can meet its counterpart. Closing from a receiver can race with active senders and make them panic. Adding a buffer may change the timing of either failure, but it does not establish ownership or cancellation. Finally, a channel does not need to be closed merely because it exists; close it when receivers need an end-of-stream signal.

Practice

Write a producer that sends the numbers 1 through 5 and then closes its channel. Write a consumer that ranges over the channel and prints the square of each number.

Summary

Channels combine communication with synchronization. Their capacity changes when a send blocks, so choose it to express a measured bound. The owner that knows all sends are finished closes the channel to announce end of stream.

Official references: channels in the language specification, send statements, and the Go memory model.

Ready to wrap up?

Mark this complete when you can explain the main idea and finish the practice task.

Copyright © 2026