github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/blog/content/pipelines/square.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  // gen sends the values in nums on the returned channel, then closes it.
     8  func gen(nums ...int) <-chan int {
     9  	out := make(chan int)
    10  	go func() {
    11  		for _, n := range nums {
    12  			out <- n
    13  		}
    14  		close(out)
    15  	}()
    16  	return out
    17  }
    18  
    19  // sq receives values from in, squares them, and sends them on the returned
    20  // channel, until in is closed.  Then sq closes the returned channel.
    21  func sq(in <-chan int) <-chan int {
    22  	out := make(chan int)
    23  	go func() {
    24  		for n := range in {
    25  			out <- n * n
    26  		}
    27  		close(out)
    28  	}()
    29  	return out
    30  }
    31  
    32  func main() {
    33  	// Set up the pipeline.
    34  	c := gen(2, 3)
    35  	out := sq(c)
    36  
    37  	// Consume the output.
    38  	fmt.Println(<-out) // 4
    39  	fmt.Println(<-out) // 9
    40  }