Remove magic numbers from code

This commit is contained in:
Richard Morrison
2019-10-28 09:01:20 +00:00
committed by Mark McGranaghan
parent 32bf0d7705
commit 0fbbc70ba5
3 changed files with 14 additions and 12 deletions

View File

@@ -27,8 +27,9 @@ func main() {
// In order to use our pool of workers we need to send
// them work and collect their results. We make 2
// channels for this.
jobs := make(chan int, 100)
results := make(chan int, 100)
const numJobs = 5
jobs := make(chan int, numJobs)
results := make(chan int, numJobs)
// This starts up 3 workers, initially blocked
// because there are no jobs yet.
@@ -38,7 +39,7 @@ func main() {
// Here we send 5 `jobs` and then `close` that
// channel to indicate that's all the work we have.
for j := 1; j <= 5; j++ {
for j := 1; j <= numJobs; j++ {
jobs <- j
}
close(jobs)
@@ -47,7 +48,7 @@ func main() {
// This also ensures that the worker goroutines have
// finished. An alternative way to wait for multiple
// goroutines is to use a [WaitGroup](waitgroups).
for a := 1; a <= 5; a++ {
for a := 1; a <= numJobs; a++ {
<-results
}
}

View File

@@ -1,2 +1,2 @@
dbe5adf9aad6828387231e477e22c5f0cd550b74
WXYS5_KpNvq
f6fd187061dfd0ae5ae8243efa3a6fcfa0777c84
hiSJJsYZJKL