github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section4/channelrange/channelrange.go (about) 1 // An example of ranging over a channel. 2 package main 3 4 import "fmt" 5 6 func main() { 7 8 // We create a buffered channel of strings with a capacity of 3 9 // This means the channel buffer can hold up to 3 values 10 messageQueue := make(chan string, 3) 11 messageQueue <- "one" 12 messageQueue <- "two" 13 messageQueue <- "three" 14 15 // Even though we close this non-empty channel, we can still receive 16 // the remaining values (see below) 17 close(messageQueue) 18 19 // We use the range keyword to iterate over each element as it gets 20 // received from the messageQueue. 21 for m := range messageQueue { 22 fmt.Println(m) 23 } 24 25 }