github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/concurrency/support/timeoutall.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 := boring("Joe") 14 timeout := time.After(5 * time.Second) // HL 15 for { 16 select { 17 case s := <-c: 18 fmt.Println(s) 19 case <-timeout: // HL 20 fmt.Println("You talk too much.") 21 return 22 } 23 } 24 } 25 // STOP1 OMIT 26 27 // START2 OMIT 28 func boring(msg string) <-chan string { // Returns receive-only channel of strings. // HL 29 c := make(chan string) 30 go func() { // We launch the goroutine from inside the function. // HL 31 for i := 0; ; i++ { 32 c <- fmt.Sprintf("%s: %d", msg, i) 33 time.Sleep(time.Duration(rand.Intn(1500)) * time.Millisecond) 34 } 35 }() 36 return c // Return the channel to the caller. // HL 37 } 38 // STOP2 OMIT 39 40 41 // START3 OMIT 42 func fanIn(input1, input2 <-chan string) <-chan string { // HL 43 c := make(chan string) 44 go func() { 45 for { 46 select { 47 case s := <-input1: 48 c <- s 49 case s := <-input2: 50 c <- s 51 } 52 } 53 }() 54 return c 55 } 56 // STOP3 OMIT