Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sync"
  6. )
  7.  
  8. func main() {
  9.  
  10. var wg sync.WaitGroup
  11. var c = make(chan string)
  12.  
  13. for i := 0; i <= 10; i++ {
  14. wg.Add(1)
  15. go func(i int) {
  16. c <- fmt.Sprintf("Event #%d", i)
  17. wg.Done()
  18. }(i)
  19. }
  20.  
  21. // Close the channel when all goroutines are finished
  22. go func() {
  23. wg.Wait()
  24. close(c)
  25. }()
  26.  
  27. for res := range c {
  28. fmt.Printf("Result: %s\n", res)
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement