Closing a channel indicates that no more values will be sent on it. This can be useful to communicate completion to the channel’s receivers. |
|
![]() ![]() package main |
|
import "fmt" |
|
In this example we’ll use a |
func main() { jobs := make(chan int, 5) done := make(chan bool) |
Here’s the worker goroutine. It repeatedly receives
from |
go func() { for { j, more := <-jobs if more { fmt.Println("received job", j) } else { fmt.Println("received all jobs") done <- true return } } }() |
This sends 3 jobs to the worker over the |
for j := 1; j <= 3; j++ { jobs <- j fmt.Println("sent job", j) } close(jobs) fmt.Println("sent all jobs") |
We await the worker using the synchronization approach we saw earlier. |
<-done } |
$ go run closing-channels.go sent job 1 received job 1 sent job 2 received job 2 sent job 3 received job 3 sent all jobs received all jobs |
|
The idea of closed channels leads naturally to our next
example: |
Next example: Range over Channels.