Goroutines

8 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 are going to be taking a deep dive into go routines. So let's get started with the program. So I'm going to make a function func heavy, which is going to be a long running task and time dot sleep. sticking into five. So this is gonna be five seconds. I think I need to import time.

Yeah. So import format. Yep. So if I call this heavy functional here, this thing so what this function is going to do is it's going to sleep for five seconds and then fitness. Gonna get started. We'll go around main door, go 12345 finished.

So function ran in waited five seconds and this heavy function was printed. Now let's see what happens when we call it as a go routine. So to call any function as a go routine, just add go keyword in front of the function, execution or function invocation. So finished. Now what is difference? So a goroutine is not a function, a go routine is the way we call a function.

So, if you call a function as a go routine, it means that the function is going to be running in the background. So, what what the function main did was, it looked at this heavy set okay call it not as a normal function, call it as a go routine and then exit start its execution in the background and don't stop whatever is below this. So earlier what was happening was when we were calling it as only a heavy function, it was waiting for five seconds This was getting executed. When we call it as go routine. The said okay executed as a go routine. And then I don't need to wait for anything because this is an asynchronous function, immediately move on to the next line and then print anything or execute any lines which come after this line.

So to see this interaction, I can see for it, I can start it and find a for loop. And I can see that okay, sleep sleep for one second and then just print anything. No, you're gonna see some strange behavior here. So we have a function heavy, which leaves for one second, and then prints Hello. Right? So and then, in our function mean, we're calling the heavy as a go routine, which means we're calling it in the background and then we're doing FMT dot readline finished.

So we took a good run and finished so what That you saw that nothing got printed. This is because this is because NGO said, okay, run it as a go routine, which means keep it in the background. And then it came over. It said, Okay, I'm going to execute this. Then after executing this, it found that function mean was empty function there wasn't anything after this, this execution in function mean. So it said okay, function mean is empty means my execution is complete I'm exiting.

So the thing about goroutines is that every goroutine is attached to a parent process. So parent process in this case is our function mean. So if the function mean exits, all of its go routines are going to exit with it. So to solve this problem, we can simply say, time dot sleep time dot second, and then C for five seconds. No different difference here is that now our go routine is going to run for an additional five seconds or four exiting. So to go run finished, hello, hello.

And our function mean is waiting and this goroutine is running in the background. So you see, you saw that only four hellos got printed in the fifth second and exited. So all of the goroutines died with it. Right. So this is not the right way. So the right way to ensure that all of the go routines are complete.

Or if you have a long running task, you can simply do a select block. So, I'm going to be talking about talking more about select block when I come to channels. For now, all you need to know is that if you do this, then this is going to wait indefinitely, our function main is going to wait indefinitely. So you do a go run, finish. Hello, hello is going to keep on running. Because, again, our go routine, or our long running task is in a for loop and it's going to keep running indefinitely until we do a Ctrl C on it.

So we do a Ctrl C So now, let me define one more task, let me say, heavy, super heavy. Right? And what funk super heavy is going to do is instead of sleeping for one second it's gonna sleep for two seconds to do and then I'm gonna see if this one is gonna print heavy. So, let's execute this function in a normal fashion first, super heavy. So, I'm just going to change it to underscore zero for number one. And so in if you're talking about design patterns and go is better if you don't have any case is very few of camelcase or things like this.

So if you look at the anatomy of our main function here, first it's calling go heavy, it's calling heavy in the background, then it's calling super heavy. Then it's calling super heavy, and then simply via printing. So, if you take a close look at super heavy, you know that this FMT dot print red fin is never going to go darker. Because this is a blocking call. The super heavy function is going to block whatever comes after this. Because this for loop is an infinite one.

So we do a go run main.go and heavy, super heavy, heavy, super heavy, because super heavy weights for two seconds and heavy weights are one second. So you get to two prints of heavy to get one super heavy. And fin never gets printed. So let's see what happens if we run our super heavy also. Go routine. So what is expected to happen when main function runs right?

So first it goes too heavy it says okay execute it as background function in the background function, it seeps for one second, then we go to super heavy, super heavy is also executed in the background where it sleeps for two seconds. And immediately in the meantime function, print line is executed and select select callers are invoked when we wait infinitely. So after one second what happens, this gets printed. And this gets one more second to go. So after one more second, heavy gets printed again and then super heavy gets printed. So these two tasks are going to keep on executing concurrently in the background.

So one, one thing to notice that this task is not going to wait for this task and vice versa. And both of these tasks are asynchronous. So our function dot format dot print line statement does not have to wait for these tasks. All right, so let's look at the execution and the output. I'm gonna do my dominant name. So Finn occurred first, as I said, then heavy, super heavy, heavy, super heavy.

Alternatively, they don't need to wait for each other, they're being executed concurrently. So guys, this was about goroutines. We're going to be talking a bit more about go routines and then eventually delve into channels in the next couple of videos. So stay tuned and see you guys there.

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.