-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
54 lines (42 loc) · 705 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/adhocore/chin"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
demo()
demoWg()
}
func demo() {
fmt.Print("custom set, no waitgroup:\n")
s := chin.New(chin.Dots)
go s.Start()
longTask()
s.Stop()
}
func demoWg() {
var wg sync.WaitGroup
fmt.Print("\ndefault set, with waitgroup:\n")
s := chin.New().WithWait(&wg)
go s.Start()
longTaskWg(&wg)
s.Stop()
wg.Wait()
}
func longTask() {
n := rand.Intn(3) + 1
fmt.Printf("[task] sleep %ds\n", n)
time.Sleep(time.Duration(n) * time.Second)
fmt.Print("[task] finished\n")
}
func longTaskWg(wg *sync.WaitGroup) {
wg.Add(1)
longTask()
wg.Done()
}