GO-vatar series: Lightweight thread
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.
But that example doesn’t show much about the asynchronous process.
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
References :