shad-go/lectures/03-goroutines/pipeline2/main.go

33 lines
406 B
Go
Raw Normal View History

2020-03-12 13:43:50 +00:00
package main
import "fmt"
func main() {
naturals := make(chan int)
squares := make(chan int)
go func() { // Counter
for x := 0; x < 100; x++ {
naturals <- x
}
close(naturals)
}()
go func() { // Squarer
for {
x, ok := <-naturals
if !ok {
break
}
squares <- x * x
}
close(squares)
}()
// Printer (in main goroutine)
for x := range squares {
fmt.Println(x)
}
}