Modern vs. Traditional Marketing

Seth Godin and Roger Kerin are two of the greatest marketing minds of our era. Although their goal is ultimately the same, to market products to consumers, their methods and ideals are couldn’t be…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Listening to Multiple Channels in Go

Welcome back to the series! Today we will look at ways to listen to multiple channels at once. The guides before helped you get started with concurrency in Go. Although simple methods are often the best ones, you may have been stuck trying to implement more complex behaviors. After reading this guide, you will be able to make your concurrent code more flexible.

We can use the select keyword to listen to multiple goroutines at once.

The code above shows how the select keyword works.

You can see that the select statement is very similar in design to the switch statement. Both define different cases and run the respective code when a certain case is met. Also, we can see that the select statement is blocking. That is, it will wait until one of the cases is met.

We iterate twice for the loop because there are only two goroutines to listen to. More exactly, each goroutine is a fire-and-forget goroutine, meaning that they only send to a channel once before returning. Therefore, there are two messages maximum at all times in this code, and we only need to select twice.

Sometimes we don’t know how many jobs there are. In this case, put the select statement inside a while loop.

Because we let a random number of goroutines run, we don’t know how many jobs there are. Thankfully, the for loop at the bottom encasing the select statement will capture every output. Let’s see what happens if we run this code.

Hmm, the select statement received three times as expected, but the program errored out due to a deadlock. Why would this be the case?

Remember that without any condition, a for loop in Go will run forever. This means that the select statement will try to receive forever. However, there is a finite number of jobs to run. Even though there are no more jobs, the select statement will still try to receive.

Remember back in the first post of the series where I said that your program will run into a deadlock if you try to receive from an unbuffered channel when the sender is not ready? This is exactly the case in our example.

So how do we solve this? We can use a combination of the concepts covered in previous posts: exit channels and WaitGroups.

The select statement is blocking by default. How do we make this non-blocking? It's simple - we just add a default case.

Aside from the lame conversation, we can see how a default case works. Instead of waiting for chats to arrive, we can do something when there are no channels to receive from. In this example, we just printed out ellipses, but you could do anything you’d like.

That’s it for this post! Now you can listen to multiple channels simultaneously, which can be a huge asset when you are developing your personal project. Thanks for reading, and I’ll see you guys next time.

Add a comment

Related posts:

PESSOAS

Como ganhar o famoso “espaço na mesa” de decisões importantes e assim, construir soluções com participantes que antes, estavam distantes. Trabalho como designer na XP Inc. há mais de 5 anos, e desde…

Best Old Gmail Accounts

There are many benefits of buying old Gmail accounts. For one, you can get access to a wealth of experience and knowledge. With an older account, you can also take advantage of Google’s extensive…

Connect the Dots

The impact of words on a child