Range over Channel in go lang!
Introduction:
Go is a popular programming language that provides support for concurrency and parallelism. One of the key features of Go is the ability to use channels to communicate between goroutines. Channels can be used to pass values and data between goroutines. The range keyword in Go can be used to iterate over channels and receive values from them. In this article, we will explore how to use range over channels in Go with examples.
Using Range over Channel:
To use range over a channel, we first need to create a channel and send some values to it. We can then use the range keyword to receive values from the channel.
Here's an example:
package main
import "fmt"
func main() {
// Create a channel
ch := make(chan string)
// Send some values to the channel
go func() {
ch <- "Hello"
ch <- "World"
ch <- "!"
close(ch)
}()
// Use range to receive values from the channel
for msg := range ch {
fmt.Println(msg)
}
}
In this example, we create a channel of type string using the make function. We then use a goroutine to send some values to the channel. The values are "Hello", "World", and "!". We close the channel after sending the values to signal that we are done sending.
We then use the range keyword to iterate over the channel and receive values from it. We store each received value in the msg variable and print it using the fmt.Println function.
The output of this program will be:
Hello
World
!
As you can see, we received all the values that were sent to the channel using the range keyword.
Using Range over Buffered Channels:
We can also use range over buffered channels in Go. Buffered channels have a buffer that can hold a certain number of values. When we use range over a buffered channel, we will receive values until the buffer is empty.
Here's an example:
package main
import "fmt"
func main() {
// Create a buffered channel
ch := make(chan int, 2)
// Send some values to the channel
ch <- 1
ch <- 2
// Use range to receive values from the channel
for i := range ch {
fmt.Println(i)
}
}
In this example, we create a buffered channel of type int with a buffer size of 2 using the make function. We then send two values, 1 and 2, to the channel.
We then use the range keyword to receive values from the channel. Since the channel has a buffer size of 2, we will receive the first two values from the channel. When we receive the third value, the channel is empty and the range loop terminates.
The output of this program will be:
1
2
Conclusion:
In this article, we learned how to use range over channels in Go to receive values from them. We also learned how to use range over buffered channels. The range keyword is a powerful tool for working with channels in Go and can simplify code that involves communicating between goroutines.
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.