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