github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/concurrency/support/faninboring.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"math/rand"
     8  	"time"
     9  )
    10  
    11  // START1 OMIT
    12  func main() {
    13  	c := fanIn(boring("Joe"), boring("Ann")) // HL
    14  	for i := 0; i < 10; i++ {
    15  		fmt.Println(<-c) // HL
    16  	}
    17  	fmt.Println("You're both boring; I'm leaving.")
    18  }
    19  // STOP1 OMIT
    20  
    21  // START2 OMIT
    22  func boring(msg string) <-chan string { // Returns receive-only channel of strings. // HL
    23  	c := make(chan string)
    24  	go func() { // We launch the goroutine from inside the function. // HL
    25  		for i := 0; ; i++ {
    26  			c <- fmt.Sprintf("%s: %d", msg, i)
    27  			time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
    28  		}
    29  	}()
    30  	return c // Return the channel to the caller. // HL
    31  }
    32  // STOP2 OMIT
    33  
    34  
    35  // START3 OMIT
    36  func fanIn(input1, input2 <-chan string) <-chan string { // HL
    37  	c := make(chan string)
    38  	go func() { for { c <- <-input1 } }() // HL
    39  	go func() { for { c <- <-input2 } }() // HL
    40  	return c
    41  }
    42  // STOP3 OMIT