Skip to content

Instantly share code, notes, and snippets.

@leyafo
Last active May 22, 2019 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leyafo/0bf3b71b14ed96d41d28303373b9c1fb to your computer and use it in GitHub Desktop.
Save leyafo/0bf3b71b14ed96d41d28303373b9c1fb to your computer and use it in GitHub Desktop.
Report mutiple go routine progress. If you wang report async goroutine schedule, you can use a channel to report the progress sequentially. Especilly if you want design a progressing bar.
package main
import (
"fmt"
"sync"
"time"
)
func main() {
c := make(chan struct{})
go func() {
processing := 0
for range c {
processing++
fmt.Printf("%d%% ", processing)
}
}()
wg := &sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
time.Sleep(1 * time.Second)
c <- struct{}{}
wg.Done()
}()
}
wg.Wait()
close(c)
fmt.Println("task Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment