This is is an example of prime number sieve
package main
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i
}
}
func Filter(in <-chan int, out chan<- int, prime int) {
for {
i := <-in
if i%prime != 0 {
out <- i
}
}
}
func main() {
ch := make(chan int)
go Generate(ch)
for i := 0; i < 10; i++ {
prime := <-ch
print(prime, "
")
ch1 := make(chan int)
go Filter(ch, ch1, prime)
ch = ch1
}
}
What I understand is this line of code
prime := <-ch
the channel is waiting for an input and assigns to prime. So, why all the numbers are not printed when the next statement is called
print(prime, "
")
If I remove these last 3 lines
ch1 := make(chan int)
go Filter(ch, ch1, prime)
ch = ch1
then all numbers are printed from 2 to 11. What does this line do ch = ch1?
Thanks