Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are channels used for?

Tags:

go

channel

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?

like image 320
Mister Verleg Avatar asked Oct 03 '16 07:10

Mister Verleg


People also ask

What are channels and Goroutines?

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.

Do Go Channels block?

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.

What is the difference between a buffered channel and an unbuffered 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.

What are channels and how do they work?

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.

What is the difference between teams and channels?

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.

What is the use of a channel in C++?

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?

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.


1 Answers

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

enter image description here

Buffered Channel

enter image description here

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/

like image 131
shivendra pratap singh Avatar answered Oct 14 '22 06:10

shivendra pratap singh