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

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"math/rand"
     8  	"time"
     9  )
    10  
    11  func main() {
    12  // START1 OMIT
    13  	c := boring("boring!") // Function returning a channel. // HL
    14  	for i := 0; i < 5; i++ {
    15  		fmt.Printf("You say: %q\n", <-c)
    16  	}
    17  	fmt.Println("You're boring; I'm leaving.")
    18  // STOP1 OMIT
    19  }
    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(1e3)) * time.Millisecond)
    28  		}
    29  	}()
    30  	return c // Return the channel to the caller. // HL
    31  }
    32  // STOP2 OMIT
    33