Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var wg sync.WaitGroup
- // Create bufferized channel with size 5
- goroutines := make(chan struct{}, 5)
- // Read data from input channel
- for data := range input {
- // 1 struct{}{} - 1 goroutine
- goroutines <- struct{}{}
- wg.Add(1)
- go func(data string, goroutines <-chan struct{}, results chan<- result, wg *sync.WaitGroup) {
- // Process data
- results <- process(data)
- // Read from "goroutines" channel to free space for new goroutine
- <-goroutines
- wg.Done()
- }(data, goroutines, results, &wg)
- }
- wg.Wait()
- close(goroutines)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement