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

27 lines
321 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++ {
naturals <- x
}
}()
go func() { // Squarer
for {
x := <-naturals
squares <- x * x
}
}()
// Printer (in main goroutine)
for {
fmt.Println(<-squares)
}
}