Goroutines
Goroutines
A Goroutine is a lightweight thread managed by the Go runtime. A Goroutine is a function or method which executes independently and simultaneously in a connection with any other Goroutine present in our program.
In other word, Goroutines are functions/methods that run concurrently with other functions/methods.
How to create a Goroutines?
We can create our own Goroutine by simply using go
keyword as prefex to the function or method.
function(x, y, z)
will start new goroutine runnning.
I will go over basics to a complex example to understand more on examples. So let’s start with basics..
Let’s assume we have this
As you can see, it will return the following output:
Now, if you try to add go keyword
in the following function:
You will get the following outputs:
As you can notice that the above program returns the continous Raj Arora
output but it doesn’t return Welcome
message because we passed go
keyword here with go displayGreetMsg("Welcome")
. The reason because:
- When a new Goroutine executed, the Goroutine call returns immediately. The control does not wait for the Goroutine to complete their execution just like normal function. So it moves forward to the next line after the Goroutine call and ignore the values.
You will be able to understand now why our Goroutine did not run. After the call go displayGreetMsg("Welcome")
, the control returned immediately to the next line of code without waiting for displayGreetMsg("Welcome")
to finish and it printed the displayGreetMsg("Raj Arora")
. And then later the main Goroutine terminated since there is no other code to execute. That’s why we see Raj Arora
as an output five times.
Let’s run this program after some modification so you will get to know what’s happening behind:
In above program, you may notice that I added a timer before printing. You will now get the following output:
The reason because:
- Now the
go displayGreetMsg("Welcome")
has enough time to execute before the main Goroutine terminates. It printsWelcome
and then wait for 1 second and printRaj Arora
and then wait for 1 second and so on.
One more example in brief
This time I am using this example which I learnt from Udemy, here we have the following program:
It will return the following output:
Now I am here using go
keyword to run our Goroutine program
Now that will put some delays to get your output:
Some advantages of Goroutine vs Threads
- We can run more Goroutines on a typical systems than threads.
- Goroutines have a faster startup time than threads.
- We can write massively concurrent servers without having to resort to evented programming.
- Goroutines are built-in primitives to communicate safely between themselves or channels.
- On Java we can run 1000’s or tens of 1000’s threads but in Go we can run hundreds of thousands or millions of Goroutines.
Hope my contribution will help you to understand this topic. Please feel free to reach out to me on twitter @rajendraarora16 for any queries.