GO-vatar series: Lightweight thread

Ferawati Hartanti Pratiwi
2 min readMay 8, 2021
https://miro.medium.com/max/11000/1*aSPCO92xpe9Z564-ExeaAw.png

Please help the Gopher with the task. :(

What is thread?

A thread is a path of execution within a process. A process can contain multiple threads. — https://www.geeksforgeeks.org/thread-in-operating-system/

In Golang, there is a mini thread called Goroutines. Goroutines are asynchronous because it can do a process simultaneously and can run multiple goroutines.

How to?

To create a Goroutines, we have to add go when call a function.

https://play.golang.org/p/w0_ORKLLREA

But that example doesn’t show much about the asynchronous process.

https://play.golang.org/p/ZRAw7b71le1
Print result

The code will print a random order (sometimes a correct order). How could this happen?

Those Goroutines run in parallel order. In looping process, there is a time to wait (we’re using sleep here) so when it stop then it could run another function (without having to wait for the entire function to finish) if called by Goroutines. This is an expected things.

Tell the Gopher about Goroutines. So the Gopher doesn’t have to wash the dishes one by one in series :D — MperMperPisang

https://miro.medium.com/max/5216/1*dD8qcmhpjUlKxZ1GHmX1AQ.png

References :

--

--

Ferawati Hartanti Pratiwi

QA (Quality Ambassador) | mpermperpisang official Medium account