Go Prime Sieve
Go routine to calculate the first 1,000 primes. You should be able to run it in the playground.
package main
import (
"fmt"
)
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i
}
}
func Filter(src <-chan int, dst chan<- int, prime int) {
for i := range src {
if i%prime != 0 {
dst <- i
}
}
}
func main() {
src := make(chan int)
go Generate(src)
for i := 0; i < 10000; i++ {
prime := <-src
println(prime)
dst := make(chan int)
go Filter(src, dst, prime)
src = dst
}
fmt.Println("done")
}