mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 06:41:33 +00:00
good good study, day day up!
This commit is contained in:
38
vendor/gopl.io/ch8/pipeline2/main.go
generated
vendored
Normal file
38
vendor/gopl.io/ch8/pipeline2/main.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 229.
|
||||
|
||||
// Pipeline2 demonstrates a finite 3-stage pipeline.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
//!+
|
||||
func main() {
|
||||
naturals := make(chan int)
|
||||
squares := make(chan int)
|
||||
|
||||
// Counter
|
||||
go func() {
|
||||
for x := 0; x < 100; x++ {
|
||||
naturals <- x
|
||||
}
|
||||
close(naturals)
|
||||
}()
|
||||
|
||||
// Squarer
|
||||
go func() {
|
||||
for x := range naturals {
|
||||
squares <- x * x
|
||||
}
|
||||
close(squares)
|
||||
}()
|
||||
|
||||
// Printer (in main goroutine)
|
||||
for x := range squares {
|
||||
fmt.Println(x)
|
||||
}
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user