Unbuffered channels

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 I'm going to be telling you about how to declare and use channels. So let's dive straight into it, to declare a channel to the name of the channel, the Chan keyword and the data type. So the name of the channel, the channel keyword, and the data type, the name of the channel, you can also be anything so in this case, I'm taking it as it can be anything you want. Right? So what this essentially means is that I created a channel with signals that can take the form of integers, right. Now if I print this channel, then you're gonna see that it prints a nil.

Why is that? Because know that VAT is used when we have to declare something not initialized anything. So here we declared a channel, which took its default value which is needed. Now there is no use for another channel. That is why what we do is we make a channel so mC mC means essentially allocating some space in memory for it. Now, if we do make, instead of our declares declaration statement, let's see what happens.

We ran this means read or go and allocated some memory to the channel. So in our case, our channel has some memory allocated to it, and it's ready to use. Now let's see how to use this channel in real life. Right? So I'm going to send something on this channel, and then I'm going to snip something out of this channel, right. So, I told you about lambda functions in go.

So how you declare them as like this, and then to call them right. Now I want to call this function as a go routine, because I want to pass in some values into the channel asynchronously, right? So the sending procedure is going to be in a in a go routine. So to send something along the channel, you simply words gonna name anything that that you want to send. Note that whatever you want to send oh here should be on the same data type. That is the data type of the channel.

So here I sent one. Now to sniff something from a channel, I can simply do value is equal to C, or the name was Janet. So in this case, the arrows pointing towards a channel, which means that I'm passing value in the channel. And this means the arrow, I'm taking some value from the channel and the arrows pointing opposite to the channel. Right. Now I can simply do a form dot print line.

So let me see. My code is correct. From dot readline. Yeah, that's what I thought. Yeah, so good on means we don't go and we had we have just one and this address got printed later because they're using coatings in our case and he printed his value before printing the actual values. Know that know that we can keep on sending things along channel.

So if we replicated this piece of code over here and then we also do winter one. And then the discord No, no doubt you already initialized well, so we only need to do is use the equal operator. Now in this code what we're doing is first we sent along we'll send something into the channel and we'll go routine. Then we sniff from that channel and then return the value. Then we did the same thing all over again we send something along channel and then we're sniffing something from the channel. So let's see what happens.

So what wondered where additional years I want to add a time notes just in case. So let's see what happens. Printed one printed two. So how this works is that in our case, this is our center goroutine. And then this is our sniffer. Right?

So whenever I'm sending something across a channel, a channel in our analogy in this case is a unbuffered channel. And unbuffered channel means size is equal to one. It means that it can hold only one value. So what we did was that Yeah, so let this be a value, right? So first, our sender sent one. Then a sniffer sniff one it said okay, I received one, print one.

Then, since our, our queue or our channel cannot hold anything except for one value. So after the consumer or the sniffer sniff the value. The sender sent another value, which is two. And then the sniffer sniff did. So in reality internally, what happens is that when you run two goroutines, at the same time, the sender can send one, what the sender cannot send to until the sniffer has sniff this one, right? So it's sniff test one.

And then you have this too. Because the size of the buffer is one, or in computer science, we say it as zero. We start all of our indexes from zero. So guys, this is the syntax of how channels work. And we did a good job again, open the file real quick for you. Yeah.

So the way that the syntax for channels is, in the next video, I'm going to be talking about buffer channels. So stay tuned and 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.