github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/concurrency/support/quit.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  	quit := make(chan bool) // HL
    14  	c := boring("Joe", quit)
    15  	for i := rand.Intn(10); i >= 0; i-- { fmt.Println(<-c) }
    16  	quit <- true // HL
    17  // STOP1 OMIT
    18  }
    19  
    20  func boring(msg string, quit <-chan bool) <-chan string {
    21  	c := make(chan string)
    22  	go func() { // HL
    23  		for i := 0; ; i++ {
    24  			time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
    25  // START2 OMIT
    26  			select {
    27  			case c <- fmt.Sprintf("%s: %d", msg, i):
    28  				// do nothing
    29  			case <-quit: // HL
    30  				return
    31  			}
    32  // STOP2 OMIT
    33  		}
    34  	}()
    35  	return c
    36  }