Goroutines continued

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

Hello world. In this video, we're going to be talking a bit more about go routines, lambda functions, and some of the best practices while executing or working with go routines. So lambda functions are functions which don't have a name, which you only need one time for a quick execute and discard. So a lambda function might be like this func and FMT dot print line. Hello. And this defines a function.

And if you want to call this function, simply do the brace and then exit, sort of go go run on Main dot main toll road go this time, and hello unfit. So we defined this function in such a way because we don't need this function after its execution. So we could even clear this function and then executed it. So if I want one more anonymous function, I might say func door. So these functions are called both Anonymous and lambda functions. So lambda functions is terminology which you might understand if you're coming from a Python background and anonymous functions if you're coming from a JavaScript background.

So go run into hello world. Now we want to run these functions as go routines. Right? So go let's look, Finn. So what happened? So this might seem like a pretty subtle thing, but you might see that hello and world are much larger strings and let's say fino here.

And there is also additional overhead of executing these functions as goroutines as compared to a normal serial execution, it might take some 0.000 microsecond difference, because of which this thing got executed before these functions did, right. So what I want to do right now is in our current paradigm, in our current paradigm, we are not waiting for the goroutines. So earlier what we will Doing was we were using the Select block right and what the Select block was doing was it was waiting indefinitely. So, when hello world and it was waiting indefinitely So, this might result in a deadlock in a lot of cases. So, if you if you take a look at this case it return return no deadlock, because our main function was also finished in this case. And then if you want to work conditions like these, we use something known as raid groups.

So raid groups are a property of the sync package in golang, which is built in so we initialize a weight group by just saying wg or the weight group is equal to sink or other The difference was sink dot weight group. So here we declare the weight group. So what weight group gives us is this add done and weight functionality so add done And wait functionality. So you're gonna see it's all going to become obvious to you after this example. So we say there w g dot add. So add to goroutines or add two functions in my wardrobe.

So you can add any function in the width group can be the go routines or functions, normal functions. So add two functions. So once my first function done, is done with this execution, you can simply say wg done. And when the second function is done with its execution, and say wg done so I added to go routines in this thing in our in my weight group, so my weight group waits for two done gods to done signals. So internally, it's divided defined as a signal. So when when it gets to done signals, then it's gonna move forward from this beach.

So what I'm saying here is you can add you can add x number of values here you can add 200,000 values over here you can add anything but for the purpose of this example, we're adding just to go routines. I'm just saying that okay, add these two go routines and wait for to done calls after to done calls, you proceed further. So we are going to wait for the to done calls. When when these are done, it means that all the functions inside of it will have been executed and terminated without any errors. And then after this, you're going to go with them. So in this case, we don't need a slave block anymore.

So if we try to run it, voila. So So you saw So you saw technologies or fin executed last because it was waiting for this thing. So if we do a print line first and then we do another print line. Is it even know what's gonna happen? Finn executive first word hello and then exit. So this is how we use raid groups, groups are a property of the sync package.

So if you have a block, if you have a block or a database or any other resource which you want only one goroutine or only one function to access at any one given time, then you can use mutexes. So, what your exes do is they ensure that every task that happens happens and in mutual exclusion of the other tasks, so only one task can happen at one time, it requires access to a shared resource. So accessing a shared resource is known as a critical section. Because a lot of things can go wrong. So for declaring mutexes, you can send mute as equal to address off sync dot txt And then you have functions like lock and unlock dot lock. And then you dot unlock.

So this is kind of like a homework for you and check check it out. It's a very cool thing and it helps you a lot when you're accessing shared resources. So, this was about raid groups and go routines and anonymous functions. 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.