Go Channels
func main() {
naturals := make(chan int)
squares := make(chan int)
done := make(chan struct{})
go Counter(naturals)
go Squarer(naturals, squares)
go Printer(squares, done)
<-done
}
func Counter(naturals chan<- int) {
for i := 0; i < 101; i++ {
naturals <- i
}
close(naturals)
}
func Squarer(naturals <-chan int, squares chan<- int) {
for {
i, ok := <-naturals
if !ok {
break
}
squares <- (i * i)
}
close(squares)
}
func Printer(squares <-chan int, done chan<- struct{}) {
for square := range squares {
fmt.Printf("Counter -> Squarer -> Printer : %d \n", square)
}
done <- struct{}{}
}