A common way to wait for a goroutine is to use a *sync.WaitGroup
:
func main() {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
// Long running task
}()
wg.Wait()
}
No problems here. However, what about this:
func main() {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
// Long running task
panic("Something unexpected happened.")
}()
wg.Wait()
}
In this case, when wg.Done()
is called, I believe main()
could exit without details of panic()
ever being written to stdout
/stderr
. Is this true and if yes, how might I prevent it from happening?