Created
December 12, 2014 15:19
-
-
Save bcho/70ed87b81d3e680e8e8c to your computer and use it in GitHub Desktop.
trick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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