Channels with space for values

The channels in the last lesson had no space inside. A send had to wait until another goroutine was ready to receive. A buffered channel has space for a few values. You give the size as the second argument to make:

package main

import "fmt"

func main() {
	jobs := make(chan string, 2)
	jobs <- "wash"
	jobs <- "dry"
	fmt.Println(len(jobs), cap(jobs))

	fmt.Println(<-jobs)
	fmt.Println(<-jobs)
}

Output:

2 2
wash
dry

Both sends finished at once, with no other goroutine, because the values went into the buffer. len gives the number of values waiting in the channel, and cap gives its size. Values come out in the order they went in, first in, first out, like people in a queue.

A full buffer waits

When the buffer is full, a send waits again, just like before. In this program the third send has no space and no receiver:

package main

func main() {
	jobs := make(chan string, 2)
	jobs <- "wash"
	jobs <- "dry"
	jobs <- "fold"
}

Output:

fatal error: all goroutines are asleep - deadlock!

A buffer does not remove the need for a receiver. It only lets the sender get a little ahead.

Closing a channel

How does the receiver know that no more values are coming? The sender closes the channel with close. A for ... range loop over a channel receives values until the channel is closed and empty:

words := make(chan string, 3)
words <- "red"
words <- "green"
words <- "blue"
close(words)

for w := range words {
	fmt.Println(w)
}

Output:

red
green
blue

Closing does not throw away the values in the buffer. The loop still received all three, and then it stopped. Without close, the loop would wait for a fourth value forever, and the program would end with the deadlock error.

You can also check by hand. A receive can return a second value, ok, which is false when the channel is closed and empty:

ch := make(chan int, 1)
ch <- 7
close(ch)

v, ok := <-ch
fmt.Println(v, ok)
v, ok = <-ch
fmt.Println(v, ok)

Output:

7 true
0 false

After the last value, the receive gives the zero value, 0, and false.

Who closes the channel

Only the sender should close a channel, and only once. Sending on a closed channel stops the program:

package main

func main() {
	ch := make(chan int, 2)
	close(ch)
	ch <- 1
}

Output:

panic: send on closed channel

Closing a channel a second time also panics, with close of closed channel. The receiver cannot know if the sender still has work, so the sender is the one that decides when the stream is over.

A common pattern puts the sender in its own goroutine. It sends every value, then closes the channel, while main reads with range:

nums := make(chan int, 3)
go func() {
	for i := 1; i <= 4; i++ {
		nums <- i * 10
	}
	close(nums)
}()
for n := range nums {
	fmt.Println(n)
}

Output:

10
20
30
40

The buffer holds only 3 values, but 4 were sent. That works because main takes values out while the goroutine puts them in.

In the exercise, send each status into the buffered channel, then close it so the range loop can finish.