Created
July 25, 2019 17:16
-
-
Save betandr/a2ae3dc090e5f62f3783dafe45052371 to your computer and use it in GitHub Desktop.
Primes generator using filters and pipes pattern
This file contains 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" | |
) | |
func main() { | |
// runtime.GOMAXPROCS(2) | |
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