Goroutines - Concurrency in Go 1
Goroutines - Concurrency in Go 1
The sleep function stops or blocks the current “thread” (goroutine) execution for the
defined duration
Now suppose the code in the function in real world takes so much time for execution
that it blocks the other entire code. Handle this blocking code with Goroutines.
Goroutines - Concurrency in Go 1
Goroutines - Concurrency in Go 2
Goroutines - Concurrency in Go 3
For concurrent code, just use the keyword “go”.
Goroutines - Concurrency in Go 4
Goroutines - Concurrency in Go 5
To perform this, first we need to create a Waitgroup before main()
var wg = sync.WaitGroup{}
Add: Sets the number of goroutines to wait for (increases the counter by the
provided number)
Goroutines - Concurrency in Go 6
Done: Decrements the WaitGroup counter by 1. So this is called by the
goroutine to indicate that it’s finished
Goroutines - Concurrency in Go 7
Goroutines - Concurrency in Go 8
Goroutines - Concurrency in Go 9