GO by example, part 4
This commit is contained in:
19
5-go-by-example/32-range-over-channels.go
Normal file
19
5-go-by-example/32-range-over-channels.go
Normal file
@@ -0,0 +1,19 @@
|
||||
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 it’s received from queue
|
||||
for elem := range queue {
|
||||
fmt.Println(elem)
|
||||
}
|
||||
// also shows that it’s possible to close a non-empty channel but still have the remaining values be received
|
||||
}
|
||||
Reference in New Issue
Block a user