Debug Go WaitGroup by Printing WaitGroup count
- To enable debug, replace the
sync.WaitGroup{}
withdebugwg.New()
- Use helper functions
wg.PeriodicDebug
orwg.Debug
to print the debug information.
package main
import (
"sync"
"time"
)
func main() {
wg := sync.WaitGroup{}
// wg := debugwg.New()
// Setup periodic print after every second
// stopPeriodicDebug := wg.PeriodicDebug(os.Stdout, 1 * time.Second)
wg.Add(1)
go func() {
defer wg.Done()
var i int32 = 0
for i = 0; i< 10; i++ {
time.Sleep(1 * time.Second)
wg.Add(1)
}
for i = 0; i< 9; i++ {
time.Sleep(1 * time.Second)
wg.Done()
}
}()
wg.Wait()
// stopPeriodicDebug()
}