Select

6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone, in this video we're going to be talking about select. So, let's dive straight into the video. So let's make a function called select. And let's pass in an argument to it, which is a channel. Now take a look at select. So select the syntax of select is exactly like the syntax of your typical switch case statement.

The only difference is for switch case does for control statements, select does four channels, select these four channels and it is async. So let's take a quick example of that. I want to run it in finite and whenever I'm running the loop, I want to check for pieces. I want to see if I received a message along the lines of C or not. And if I did, I'm going to append to C. And what I'm going to do is I'm going to create one more channel for you guys. I'm going to call your Chad, which is gonna be a four type struct.

No, I never have to see something on channel. See, I'm gonna say to see, whenever I receive something on grids, I do a quick print line and I'll say quit. And then I do a break. So this break statement is going to break out to the for loop, so that we can exit our program all over again. So this is a really great way of making sure that your tasks are running indefinitely. So I'm going to make two terms over here.

I'm gonna make C and I'm gonna leave I can have any name I want. I'm just gonna I'm just gonna call it quits, because why not? So these two channels what I'm gonna do now is I'm going to pass them to the Select block directly. Note that when I'm passing those channels or any channel, in fact, to a function, I don't have to pass the reference of the channel, I can pass the exact channel because internally challenge or challenge channels are references only. So I don't need to worry about that anymore. Then I can do a quick go phone.

I can run the Select block in the background. Okay, and then I can do or I can make function to pass in arguments along those standards. Now, let's see what happens in this case. I'm going to do a quick go run, and nothing happened. So why is that? The thing is that both of the both of our thing, both functions were executed as co routines, but we didn't have anything to block our main function.

So to block our main function, I'm going to have another video Select block, which is gonna make sure that is going to stay blocked. Now, in the past couple of videos, we use a select block, but we never really talked about why we use this empty select block. So, an empty select block means that, okay, I want to switch case on my channel, but I don't know which channels to switch case on. So I'm just gonna wait indefinitely. Because in a in a field, select blog, we have cases where we can listen on channels in an empty select blog, we don't have any channels to listen on. So it's just like blocking a thread.

So I'm going to do a bit going on all over again. And it received an error saying that all go routines are asleep. Because no cases were executed. This is because our select block was actually called before this score. So to solve this, what we can do is we can do a time dot sleep for one second you know, let's see what happens. It worked.

Domain six go all over again. Now why did it fail the second time? The reason it failed the second time was because when we pass something to the channel see it. For the first time it received something is said, Okay, I received it. When the loop continued all over again, it tried to listen on the C channel or all over again. But notice that our C channel is a unbuffered channel, which means its buffer size is zero, it can only hold one item at one time.

So what happened what happened was that we didn't send anything after this, so it immediately quit. So what we can do is we can do quits, you can send something along the line of codes to exit the program as soon as we've sent something along channels, so after doing that, what we're gonna do is insert a public statement I'm just gonna exit. So for that there is a function called always dot exit. And in that we can say that What a status code is going to be. So after we insert our status code is going to be one because we quit on purpose. Or rather 00 means it's safe.

Then I'm going to clear all of this out, and then I'm going to clean code on. So first we receive something along the channel, then we waited for one second, then we receive something wrong the quit channel, and then we quit. So let's see what happens when we increase the buffer size of this see. Now in this case, when we empty, we do a go run in sixth. So the first time we received a message in the second time, it also threw in error. Why?

So some of you might be thinking that our buffer size is two right now. But so why is it throwing an error now? The reason is that we only sent centered first time. So even though our buffer sizes two, there is only one item in the buffer right now, when this gets consumed, there is no other item to consume. So it panics so that is this was about the Select block select block is really great for controlling execution, where you want to control bulk execution by task queues and things like that. So, I request you guys to read more on this.

The links are going to be in the resource section. See you guys in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.