mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-05 14:03:45 +00:00
38 lines
534 B
Go
38 lines
534 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 228.
|
|
|
|
// Pipeline1 demonstrates an infinite 3-stage pipeline.
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
//!+
|
|
func main() {
|
|
naturals := make(chan int)
|
|
squares := make(chan int)
|
|
|
|
// Counter
|
|
go func() {
|
|
for x := 0; ; x++ {
|
|
naturals <- x
|
|
}
|
|
}()
|
|
|
|
// Squarer
|
|
go func() {
|
|
for {
|
|
x := <-naturals
|
|
squares <- x * x
|
|
}
|
|
}()
|
|
|
|
// Printer (in main goroutine)
|
|
for {
|
|
fmt.Println(<-squares)
|
|
}
|
|
}
|
|
|
|
//!-
|