Skip to content

Instantly share code, notes, and snippets.

@jcarley
Created August 9, 2015 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcarley/52550a3245e10c204ba7 to your computer and use it in GitHub Desktop.
Save jcarley/52550a3245e10c204ba7 to your computer and use it in GitHub Desktop.
Go Pipe and Filter
package workers
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(4)
ch := make(chan int)
go generate(ch)
for {
prime := <-ch
fmt.Println(prime)
ch1 := make(chan int)
go filter(ch, ch1, prime)
ch = ch1
}
}
func generate(ch chan int) {
for i := 2; ; i++ {
ch <- i
}
}
func filter(in, out chan int, prime int) {
for {
i := <-in
if i%prime != 0 {
out <- i
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment