github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/concurrency/support/generator2boring.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  	joe := boring("Joe") // HL
    14  	ann := boring("Ann") // HL
    15  	for i := 0; i < 5; i++ {
    16  		fmt.Println(<-joe)
    17  		fmt.Println(<-ann)
    18  	}
    19  	fmt.Println("You're both boring; I'm leaving.")
    20  }
    21  // STOP1 OMIT
    22  
    23  // START2 OMIT
    24  func boring(msg string) <-chan string { // Returns receive-only channel of strings. // HL
    25  	c := make(chan string)
    26  	go func() { // We launch the goroutine from inside the function. // HL
    27  		for i := 0; ; i++ {
    28  			c <- fmt.Sprintf("%s: %d", msg, i)
    29  			time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
    30  		}
    31  	}()
    32  	return c // Return the channel to the caller. // HL
    33  }
    34  // STOP2 OMIT
    35