• author: CodeDonor

Using Channels in Go: Communicating Between Different Goroutines

In the previous video, we saw that the main routine exits as soon as it creates all its child goroutines. However, the child routines are still running, and we need a way to communicate between them and the main routine. One way to solve this issue is to use a channel, which is a built-in communication primitive in Go.

Creating a Channel

To create a new channel in Go, we can use the make keyword, followed by the type of data that we want to communicate over this channel. For example, to create a channel that communicates with values of type string, we can use the following syntax:

C := make(chan string)

As with any other variable in Go, we need to pass this channel around our application to use it in different functions and routines.

Sending and Receiving Data in a Channel

We can think of a channel as a two-way messaging device, where one entity sends a message, and the other entity receives it. To send data to a channel, we can use the little arrow symbol <- followed by the value that we want to send. For example, to send the number 5 to a channel C, we can use the following syntax:

C <- 5

On the receiving end, we can use the same arrow symbol followed by a variable that will receive the value from the channel. For example, to receive a value from a channel C and store it in a variable myNumber, we can use the following syntax:

myNumber := <-C

Alternatively, we can receive a value from a channel and pass it as an argument to a function. For example:

fmt.Println(<-C)

Communicating Between Goroutines

To communicate between different goroutines using a channel, we need to pass the channel as an argument to each of the goroutines that need to communicate. For example, let's say we have a function checkLink that checks whether a given website is up or down. We can pass the channel C as a second argument to this function, like so:

go checkLink(link, C)

Then, inside this checkLink function, we can send messages to the channel C as needed, for example:

C <- "Website might be down"

On the receiving end, we can use a for loop to receive all the messages from the channel until the channel is closed. For example:

for msg := range C {
    fmt.Println(msg)
}

Rules and Oddities

Using channels in Go is not without its rules and oddities. Here are a few things to keep in mind:

  • The data or messages that we send through a channel must always be of the same type as specified when we created the channel.
  • Channels are unbuffered by default, which means that sending a message to a channel blocks until someone is ready to receive that message.
  • Closing a channel is optional, but it can signal to the receiver that no more messages will be sent on this channel. Attempting to send a message to a closed channel will result in a runtime error.
  • Select statements can be used to communicate between channels in a non-blocking way, allowing for more complex communication patterns between different goroutines.
  • Channels can also be used to synchronize the execution of different goroutines by waiting for a message from one goroutine before starting another one.

Conclusion

Using channels in Go is a powerful way to communicate between different goroutines and synchronize their execution. By following the rules and using the appropriate syntax, we can build complex concurrent applications that take full advantage of Go's concurrency primitives.

Previous Post

Understanding Go Routines: Concurrency vs. Parallelism

Next Post

Understanding the Relevance of iFrame Stuff

About The auther

New Posts

Popular Post