Debian Codegraph @

example/incr/incr.go

raw
1 package main
2
3 import (
4 "fmt"
5 "math/rand"
6 "runtime"
7 "sync"
8 "time"
9
10 "github.com/gosuri/uiprogress"
11 )
12
13 func main() {
14 runtime.GOMAXPROCS(runtime.NumCPU()) // use all available cpu cores
15
16 // create a new bar and prepend the task progress to the bar and fanout into 1k go routines
17 count := 1000
18 bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed()
19 bar.PrependFunc(func(b *uiprogress.Bar) string {
20 return fmt.Sprintf("Task (%d/%d)", b.Current(), count)
21 })
22
23 uiprogress.Start()
24 var wg sync.WaitGroup
25
26 // fanout into 1k go routines
27 for i := 0; i < count; i++ {
28 wg.Add(1)
29 go func() {
30 defer wg.Done()
31 time.Sleep(time.Millisecond * time.Duration(rand.Intn(500)))
32 bar.Incr()
33 }()
34 }
35 time.Sleep(time.Second) // wait for a second for all the go routines to finish
36 wg.Wait()
37 uiprogress.Stop()
38 }