Skip to content

Instantly share code, notes, and snippets.

@bcho
Created December 12, 2014 15:19
Show Gist options
  • Select an option

  • Save bcho/70ed87b81d3e680e8e8c to your computer and use it in GitHub Desktop.

Select an option

Save bcho/70ed87b81d3e680e8e8c to your computer and use it in GitHub Desktop.
trick
package main
import (
"fmt"
"sync"
)
func piper(left chan int, wg *sync.WaitGroup) {
forRight := make(chan int, 0)
wg.Add(1)
go (func(left, right chan int) {
defer wg.Done()
needNewPipe := true
number := <-left
fmt.Printf("%d ", number)
for newNumber := range left {
if newNumber%number != 0 {
if needNewPipe {
needNewPipe = false
go piper(forRight, wg)
}
forRight <- newNumber
}
}
// Notify right piper we are done.
close(forRight)
})(left, forRight)
}
func main() {
var wg sync.WaitGroup
init := make(chan int, 0)
go piper(init, &wg)
for i := 2; i < 1000; i++ {
init <- i
}
close(init)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment