forked from sbotman/go-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise2.go
68 lines (55 loc) · 1.59 KB
/
exercise2.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
59
60
61
62
63
64
65
66
67
68
// All material is licensed under the GNU Free Documentation License
// https://github.com/disney/go-training/blob/master/LICENSE
// http://play.golang.org/p/B9npiUVveE
// Write a problem that uses a buffered channel to maintain a buffer
// of four strings. In main, send the strings 'A', 'B', 'C' and 'D'
// into the channel. Then create 20 goroutines that receive a string
// from the channel, display the value and then send the string back
// into the channel. Once each goroutine is done performing that task,
// allow the goroutine to terminate.
package main
import (
"fmt"
"sync"
)
const (
goroutines = 20
capacity = 4
)
var (
// wg is used to wait for the program to finish.
wg sync.WaitGroup
// resources is a buffered channel to manage resources.
resources = make(chan string, capacity)
)
// main is the entry point for all Go programs.
func main() {
// Launch goroutines to handle the work.
wg.Add(goroutines)
for gr := 1; gr <= goroutines; gr++ {
go worker(gr)
}
// Add the resources.
for rune := 'A'; rune < 'A'+capacity; rune++ {
resources <- string(rune)
}
// Wait for all the work to get done.
wg.Wait()
}
// worker is launched as a goroutine to process work from
// the buffered channel.
func worker(worker int) {
// Report that we just returned.
defer wg.Done()
// Receive a resource from the channel.
value, ok := <-resources
if !ok {
// This means the channel is empty and closed.
fmt.Printf("Worker: %d : Shutting Down\n", worker)
return
}
// Display the value.
fmt.Printf("Worker: %d : %s\n", worker, value)
// Place the resource back.
resources <- value
}