Buffered channels

5 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 buffer channels. So what buffer channels means is the size, or the amount of values that they can store is greater than zero or greater than one. So sizes three, which means that sender can send three items concurrently. And the sniffer doesn't have to sniff any anything by then. And after that, the sniffer can sniff items one by one and the sender can send it so let's say the size is three you have three items in the queue right now the sniffer will sniff one item and then you have two items in the queue.

So the sender can produce one more item right? So you're gonna see this analogy in player diamond or dim mean three dot mean folder. Right. So what we did earlier was we did a C is equal to mC mC Chan. So the MC. So the main keyword takes another argument which is the buffer size.

So I can say let the buffer size with You know, I quickly make three goroutines to send along those buffer lines, so I can say okay sin sin one sin, sin. And then same for forward, you know, what we can do is we can also range over the channel. So we can say that for i is equal to range C from 29. I know, let's see what happens in this case. So, let's see if this throws an error. And so what happened after it, why did it stop working after four tries?

So if you look at this code real quick, I'm sorry guys, I have a bad throat So if you look at this code, you're going after four retries. It's not doing anything, right, so we can close this channel. Now when we close this channel, it doesn't have, it doesn't have to read over it anymore. So it can simply say that okay, the channel is now closed. And I don't need to read over it anymore. So, the I, the iterator will arrange over the channel till the channel is closed.

In our go routine, go routine, we sent three values. And then immediately after sending the three and four values, we close the channel, you said that, okay, we don't want to send anything after it. And then when we were reading on the channel, we said, okay, the channel was closed over here, we know where to reinstate. So I'll keep on reading to the channel and I'll keep on printing every value which was in the channel. So, in our case, in our case, you know, our buffer or a queue was able to store three values. And what happened was that after the sniffers sniff the first value, it ranged over all the other values The sender was able to send this for this message for the signal for over to the channel.

And then after sending for our sniffer sniffed our two, then it sniffed our three, and then it stripped off four. And then since the channel was closed, then it didn't need to proceed any further. So till now we only did everything using integer types. We can also have self defined types over here. So I can say type God struct where I can have a more string then I can also have a reference to the model or a reference to the car. Then I can say, okay, fast enough God.

Good margin number one. And similarly, I can do 234 I can do a three night So after sending, after sending a pointer to the car struct to the Channel Four times I closed the channel, I arrange all the channel, notice that this is asynchronous. So this is going to run before running this. So our for loop is gonna range, every channel is closed. And then we can print all of these values that we are receiving over here. So we can say i dot model.

So quickly running the score, you can see that it looks like a job. So this was about buffer channels. And this is how you make buffer channels. You use the make command make keyword, you make a channel with a particular data type, and make sure to pass in only the data type that you have declared over here. And then you can pass it pass in your second argument to the main keyword or the main function where you can decide What the buffer size is going to be. So thank you guys and see you in the next video.

Stay tuned.

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.