Range over Channels
package main
import "fmt"
func main() {
// Creating a buffered channel 'queue' with a capacity of 2
queue := make(chan string, 2)
// Sending two elements ("one" and "two") to the 'queue' channel
queue <- "one"
queue <- "two"
// Closing the 'queue' channel to indicate that no more elements will be sent
close(queue)
// Iterating over the elements in the closed 'queue' channel using 'range'
for elem := range queue {
fmt.Println(elem)
}
}Output
Last updated