Search⌘ K
AI Features

Puzzle 16 Explanation: Channel Capacity

Explore how buffered channels operate in Go by sending and receiving values, handling closed channels, and using the comma ok pattern to distinguish valid data from zero values.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Go (1.16.5)
package main
import (
"fmt"
)
func main() {
ch := make(chan int, 2)
ch <- 1
ch <- 2
<-ch
close(ch)
a := <-ch
b := <-ch
fmt.Println(a, b)
}

Explanation

The ch channel is a buffered channel with a capacity of 2 (we can check this ...