forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template1.go
56 lines (44 loc) · 1.02 KB
/
template1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Fix the race condition in this program.
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
// numbers maintains a set of random numbers.
var numbers []int
// init is called prior to main.
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
// Number of goroutines to use.
const grs = 3
// wg is used to manage concurrency.
var wg sync.WaitGroup
wg.Add(grs)
// Create three goroutines to generate random numbers.
for i := 0; i < grs; i++ {
go func() {
random(10)
wg.Done()
}()
}
// Wait for all the goroutines to finish.
wg.Wait()
// Display the set of random numbers.
for i, number := range numbers {
fmt.Println(i, number)
}
}
// random generates random numbers and stores them into a slice.
func random(amount int) {
// Generate as many random numbers as specified.
for i := 0; i < amount; i++ {
n := rand.Intn(100)
numbers = append(numbers, n)
}
}