Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. var wg sync.WaitGroup
  2. // Create bufferized channel with size 5
  3. goroutines := make(chan struct{}, 5)
  4. // Read data from input channel
  5. for data := range input {
  6. // 1 struct{}{} - 1 goroutine
  7. goroutines <- struct{}{}
  8. wg.Add(1)
  9. go func(data string, goroutines <-chan struct{}, results chan<- result, wg *sync.WaitGroup) {
  10. // Process data
  11. results <- process(data)
  12. // Read from "goroutines" channel to free space for new goroutine
  13. <-goroutines
  14. wg.Done()
  15. }(data, goroutines, results, &wg)
  16. }
  17.  
  18. wg.Wait()
  19. close(goroutines)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement