Continuing with our lockdown learning of Go language. Here’s a video on the Need to wait for goroutines in Golang
A goroutine may have to wait for another goroutine to complete. Using time.Sleep() is a very crude way to wait
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
go fixIssues()
time.Sleep(time.Second * 5) //Unreliable.
//So it would be good for the main goroutine to wait for fixIssues goroutine to complete
//How do we do that? Next video
fmt.Println("***Enough! I am done waiting")
}
func fixIssues() {
i := 0
rand.Seed(time.Now().UnixNano())
iterations := rand.Intn(20)
fmt.Println("Time required:", iterations, "seconds")
for i < iterations {
fmt.Println("Fixing Issues ...")
time.Sleep(time.Second * 1)
i++
}
}
You can watch other videos in Go lang in the links below