Created
August 30, 2018 15:23
-
-
Save dulao5/a7dda68654ff02ad378c80e88d5d9407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://yar999.gitbooks.io/gopl-zh/content/ch8/ch8-04.html | |
// gopl.io/ch8/pipeline3 | |
package main | |
import ( | |
"fmt" | |
//"sync" | |
) | |
func counter(out chan<- int) { | |
for x := 0; x < 100; x++ { | |
out <- x | |
fmt.Println("A. counter sent", x) | |
} | |
close(out) | |
} | |
func squarer(in <-chan int, out chan<- int) { | |
for v := range in { | |
out <- v * v | |
fmt.Println("B. squarer received", v) | |
} | |
close(out) | |
} | |
func printer(in <-chan int, outQuit chan<- int) { | |
for v := range in { | |
fmt.Println("C. printer received", v) | |
} | |
outQuit <- 0 | |
} | |
func main() { | |
naturals := make(chan int) | |
squares := make(chan int) | |
quit := make(chan int) | |
// Counter | |
go counter(naturals) | |
// Squarer | |
go squarer(naturals, squares) | |
// Printer | |
go printer(squares, quit) | |
<-quit | |
fmt.Println("main received quit") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment