March 2020
Intermediate to advanced
406 pages
8h 39m
English
You may want to know all the values present in your buffered channel. We have the ability to do this by invoking a range built-in over the channel we'd like to check. Our example in the following code block adds three elements to the channel, closes the channel, and then writes all the elements from the channel using fmt:
package main import "fmt" func main() { bufferedChannel := make(chan int, 3) bufferedChannel <- 1 bufferedChannel <- 3 bufferedChannel <- 5 close(bufferedChannel) for i := range bufferedChannel { fmt.Println(i) } }
The resulting output shows us all of the values that live in our buffered channel:

Read now
Unlock full access