Here in main, we are waiting/reading from channel “ch1” and this waiting/reading will complete when someone will write something to this channel. So once the goroutine hello() complete its execution and write “true” to the channel then in the main it will able to read from the channel and executes further. Thus the output will be:–
My Hello world goroutine
in main function, returned value is true
Thus instead of using time.sleep(), we can achieve it using channels and this is one of the best way to resolve it.
WaitGroup:—
Using Waitgroup, we can wait for multiple goroutines to finish their work. Thus the control is blocked until all Goroutines finish there execution