Guest User

Untitled

a guest
Aug 24th, 2019
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4. "log"
  5. "sync"
  6. )
  7.  
  8. func main() {
  9.  
  10. inChan := make(chan workToDo, 2)
  11. outChan := make(chan workResult, 2)
  12. var wg sync.WaitGroup
  13.  
  14. wg.Add(1)
  15. go startWorker(inChan, outChan, &wg)
  16.  
  17. wg.Add(1)
  18. go startWorker(inChan, outChan, &wg)
  19.  
  20. go func() {
  21. inChan <- workToDo{messageIn: "foo"}
  22. inChan <- workToDo{messageIn: "bar"}
  23. inChan <- workToDo{messageIn: "baz"}
  24. close(inChan)
  25. }()
  26.  
  27. go func() {
  28. wg.Wait()
  29. close(outChan)
  30. }()
  31.  
  32. for v := range outChan {
  33. log.Printf("%#v\n", v)
  34. }
  35.  
  36. }
  37.  
  38. type workToDo struct {
  39. messageIn string
  40. }
  41. type workResult struct {
  42. messageOut string
  43. }
  44.  
  45. func startWorker(in chan workToDo, out chan workResult, wg *sync.WaitGroup) {
  46. for v := range in {
  47. log.Printf("%#v\n", v)
  48. out <- workResult{messageOut: "Echo: " + v.messageIn}
  49. }
  50. wg.Done()
  51. }
RAW Paste Data