Created
January 27, 2015 03:28
-
-
Save glesica/a2cd983fa1b4400c158e to your computer and use it in GitHub Desktop.
Simple example of a goroutine pool.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
numbers := make(chan int, 10) | |
squares := make(chan int, 10) | |
squareGroup := new(sync.WaitGroup) | |
printGroup := new(sync.WaitGroup) | |
// Create 1 worker to print the squares | |
printGroup.Add(1) | |
go func() { | |
for square := range squares { | |
fmt.Println(square) | |
} | |
printGroup.Done() | |
}() | |
// Create 4 workers to square numbers | |
for i := 0; i < 4; i++ { | |
squareGroup.Add(1) | |
go func() { | |
for number := range numbers { | |
squares <- (number * number) | |
} | |
squareGroup.Done() | |
}() | |
} | |
// Square some numbers! | |
for i := 0; i < 10; i++ { | |
numbers <- i | |
} | |
// Wait for all the numbers to be squared | |
close(numbers) | |
squareGroup.Wait() | |
// Wait for all the squares to be printed | |
close(squares) | |
printGroup.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment