When looking through some Go code I found the following:
ch := make(chan int)
I looked up in a online tutorial how Go Channels work:
https://tour.golang.org/concurrency/2
But I find this example unclear.
Can someone give me a easy explanation and an example of the use of channels?
A channel is a communication object using which goroutines can communicate with each other. Technically, a channel is a data transfer pipe where data can be passed into or read from. Hence one goroutine can send data into a channel, while other goroutines can read that data from the same channel.
When we send data into the channel using a GoRoutine, it will be blocked until the data is consumed by another GoRoutine. When we receive data from channel using a GoRoutine, it will be blocked until the data is available in the channel.
When a channel is created with no capacity, it is called an unbuffered channel. In turn, a channel created with capacity is called a buffered channel.
Channels are dedicated sections within a team to keep conversations organized by specific topics, projects, disciplines—-whatever works for your team! Files that you share in a channel (on the Files tab) are stored in SharePoint.
Teams are built on Microsoft 365 Groups, and changes to Microsoft 365 group membership sync to the team. Channels are the collaboration spaces within a team in which the actual work is done. To read more about teams and channels, see the Overview of teams and channels in Microsoft Teams.
A channel provides a mechanism for concurrently executing functions to communicate by sending and receiving values of a specified element type. When you have multiple goroutines which are executed concurrently, channels provide the easiest way to allow the goroutines to communicate with each other.
What are communication channels? Communication channels are tools used by companies to establish a relationship and communicate with their audience. They enhance the experience between the customer and the brand, boosting relationship marketing, generating recognition for the company and impacting sales.
chan is a channel in Golang. In simple word you can think it as a box in which you put a item at one end and then pick it from other end.
Unbuffered Channels
Buffered Channel
This is the small code I have written for you to understand channels. Now change order of go routines and see the outputs. Each time output may differ.
package main import ( "fmt" "time" ) func main() { messages := make(chan int) go func() { time.Sleep(time.Second * 3) messages <- 1 }() go func() { time.Sleep(time.Second * 2) messages <- 2 }() go func() { time.Sleep(time.Second * 1) messages <- 3 }() go func() { for i := range messages { fmt.Println(i) } }() go func() { time.Sleep(time.Second * 1) messages <- 4 }() go func() { time.Sleep(time.Second * 1) messages <- 5 }() time.Sleep(time.Second * 5) }
For best understanding visit this blog where go routines and channels are described in GUI.
Visit http://divan.github.io/posts/go_concurrency_visualize/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With