go_playground/5-go-by-example/32-range-over-channels.go

20 lines
453 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import "fmt"
func main() {
// iterate over values received from a channel
// here iterate over 2 values in the queue channel
queue := make(chan string, 2)
queue <- "one"
queue <- "two"
close(queue)
// range iterates over each element as its received from queue
for elem := range queue {
fmt.Println(elem)
}
// also shows that its possible to close a non-empty channel but still have the remaining values be received
}