-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgoroutines.go
58 lines (48 loc) · 1.58 KB
/
goroutines.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
55
56
57
58
// Create two anonymous functions: one that outputs integers from 1 to 100; the
// other from 100 to 1. Start each function as a goroutine. Use a WaitGroup
// to ensure that main() doesn't exit until the goroutines are done.
package main
// Add your imports here.
import (
"fmt"
"sync"
"time"
)
func main() {
// Create a new waitgroup.
var wg sync.WaitGroup
// Add 2 to the waitgroup; one for each counter function.
wg.Add(2)
// Create an anonymous function that counts from 1 to 100, and launch it as
// a goroutine.
go func(id string) {
// Schedule the waitgroup to be decremented when the function exists.
defer wg.Done()
// Loop from 1 to 100.
for i := 1; i <= 100; i++ {
// Use `time.Sleep` to simulate doing some work so the scheduler
// has the chance to switch between goroutines.
time.Sleep(time.Millisecond * 10)
// Output the current number, prefixed with a value that identifies
// this function.
fmt.Println(id, i)
}
}("up")
// Create an anonymous function that counts from 1 to 100, and launch it as
// a goroutine.
go func(id string) {
// Schedule the waitgroup to be decremented when the function exists.
defer wg.Done()
// Loop from 100 to 1.
for i := 100; i >= 1; i-- {
// Use `time.Sleep` to simulate doing some work so the scheduler
// has the chance to switch between goroutines.
time.Sleep(time.Millisecond * 10)
// Output the current number, prefixed with a value that identifies
// this function.
fmt.Println(id, i)
}
}("down")
// Wait until both goroutines have finished before existing main().
wg.Wait()
}