Kickjkee

Untitled

Oct 25th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.88 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "sync"
  6.     "time"
  7. )
  8.  
  9. type job func(out chan interface{})
  10.  
  11. var executePipeline = func(jobs ...job) chan interface{} {
  12.     out := make(chan interface{})
  13.     wg := &sync.WaitGroup{}
  14.     for _, j := range jobs {
  15.         wg.Add(1)
  16.         go func(j job) {
  17.             j(out)
  18.             wg.Done()
  19.         }(j)
  20.     }
  21.     go func() {
  22.         wg.Wait()
  23.         close(out)
  24.     }()
  25.     return out
  26. }
  27.  
  28. func main() {
  29.     start := time.Now()
  30.  
  31.     rr := executePipeline(func(out chan interface{}) {
  32.         time.Sleep(time.Millisecond * 100)
  33.         out <- "1"
  34.     }, func(out chan interface{}) {
  35.         time.Sleep(time.Millisecond * 200)
  36.         out <- "2"
  37.     }, func(out chan interface{}) {
  38.         time.Sleep(time.Millisecond * 300)
  39.         out <- "3"
  40.     }, func(out chan interface{}) {
  41.         time.Sleep(time.Millisecond * 400)
  42.         out <- "4"
  43.     })
  44.  
  45.     for r := range rr {
  46.         fmt.Println(r.(string))
  47.     }
  48.  
  49.     fmt.Printf("time since start: %v \n", time.Since(start))
  50. }
Advertisement
Add Comment
Please, Sign In to add comment