Skip to content

Closing channels

You can close a channel with close(ch). Receivers can test whether a channel is closed using the comma‑ok idiom: v, ok := <-ch. Sending on a closed channel causes a panic.

Close a channel and detect the closure.

package main
import "fmt"
func main() {
ch := make(chan int, 2)
ch <- 1
ch <- 2
close(ch)
for i := 0; i < 3; i++ {
v, ok := <-ch
fmt.Printf("value=%d, ok=%t\n", v, ok)
}
}
Terminal window
value=1, ok=true
value=2, ok=true
value=0, ok=false