Range over Channel in go lang!
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...

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.
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.
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
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.