mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 14:42:01 +00:00
good good study, day day up!
This commit is contained in:
42
vendor/gopl.io/ch8/pipeline3/main.go
generated
vendored
Normal file
42
vendor/gopl.io/ch8/pipeline3/main.go
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 231.
|
||||
|
||||
// Pipeline3 demonstrates a finite 3-stage pipeline
|
||||
// with range, close, and unidirectional channel types.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
//!+
|
||||
func counter(out chan<- int) {
|
||||
for x := 0; x < 100; x++ {
|
||||
out <- x
|
||||
}
|
||||
close(out)
|
||||
}
|
||||
|
||||
func squarer(out chan<- int, in <-chan int) {
|
||||
for v := range in {
|
||||
out <- v * v
|
||||
}
|
||||
close(out)
|
||||
}
|
||||
|
||||
func printer(in <-chan int) {
|
||||
for v := range in {
|
||||
fmt.Println(v)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
naturals := make(chan int)
|
||||
squares := make(chan int)
|
||||
|
||||
go counter(naturals)
|
||||
go squarer(squares, naturals)
|
||||
printer(squares)
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user