-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoroutines02.go
More file actions
47 lines (32 loc) · 865 Bytes
/
goroutines02.go
File metadata and controls
47 lines (32 loc) · 865 Bytes
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
/* Alta3 Research | RZFeeser
goroutine - with waitgroups */
package main
import (
"fmt"
"time"
"sync"
)
func countDown(s int) {
defer wg.Done()
for i := s; i > 0; i-- {
fmt.Println(i)
time.Sleep(time.Second) // wait one second
}
}
// WaitGroup is used to wait for the program to finish goroutines
var wg sync.WaitGroup
func main() {
fmt.Println("NASA launch sequence starting:")
// the waitGroup has "1" in the queue
wg.Add(1)
// Begin the countdown
go countDown(10)
fmt.Println("Launch sequence starting:")
time.Sleep(time.Second)
fmt.Println("Hey wait a second...")
time.Sleep(time.Second)
fmt.Println("I forgot my wallet!")
// wait for the waitGroup queue to reach 0
wg.Wait()
fmt.Println("TAKE OFF!")
}