aboutsummaryrefslogtreecommitdiff
path: root/content/pipelines/square2.go
diff options
context:
space:
mode:
Diffstat (limited to 'content/pipelines/square2.go')
-rw-r--r--content/pipelines/square2.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/content/pipelines/square2.go b/content/pipelines/square2.go
new file mode 100644
index 0000000..4530fea
--- /dev/null
+++ b/content/pipelines/square2.go
@@ -0,0 +1,35 @@
+package main
+
+import "fmt"
+
+// gen sends the values in nums on the returned channel, then closes it.
+func gen(nums ...int) <-chan int {
+ out := make(chan int)
+ go func() {
+ for _, n := range nums {
+ out <- n
+ }
+ close(out)
+ }()
+ return out
+}
+
+// sq receives values from in, squares them, and sends them on the returned
+// channel, until in is closed. Then sq closes the returned channel.
+func sq(in <-chan int) <-chan int {
+ out := make(chan int)
+ go func() {
+ for n := range in {
+ out <- n * n
+ }
+ close(out)
+ }()
+ return out
+}
+
+func main() {
+ // Set up the pipeline and consume the output.
+ for n := range sq(sq(gen(2, 3))) {
+ fmt.Println(n) // 16 then 81
+ }
+}