github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/concurrency/support/rcvquit.go (about) 1 // +build OMIT 2 3 package main 4 5 import ( 6 "fmt" 7 "math/rand" 8 "time" 9 ) 10 11 func cleanup() { 12 } 13 14 func main() { 15 // START1 OMIT 16 quit := make(chan string) // HL 17 c := boring("Joe", quit) // HL 18 for i := rand.Intn(10); i >= 0; i-- { fmt.Println(<-c) } 19 quit <- "Bye!" // HL 20 fmt.Printf("Joe says: %q\n", <-quit) // HL 21 // STOP1 OMIT 22 } 23 24 func boring(msg string, quit chan string) <-chan string { 25 c := make(chan string) // HL 26 go func() { 27 for i := 0; ; i++ { 28 time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) 29 // START2 OMIT 30 select { 31 case c <- fmt.Sprintf("%s: %d", msg, i): 32 // do nothing 33 case <-quit: // HL 34 cleanup() 35 quit <- "See you!" // HL 36 return 37 } 38 // STOP2 OMIT 39 } 40 }() 41 return c 42 }