github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/waza/snippets (about) 1 f("hello", "world") // f runs; we wait 2 3 go f("hello", "world") // f starts running 4 g() // does not wait for f to return 5 6 timerChan := make(chan time.Time) 7 go func() { 8 time.Sleep(deltaT) 9 timerChan <- time.Now() // send time on timerChan 10 }() 11 // Do something else; when ready, receive. 12 // Receive will block until timerChan delivers. 13 // Value sent is other goroutine's completion time. 14 completedAt := <-timerChan 15 16 select { 17 case v := <-ch1: 18 fmt.Println("channel 1 sends", v) 19 case v := <-ch2: 20 fmt.Println("channel 2 sends", v) 21 default: // optional 22 fmt.Println("neither channel was ready") 23 } 24 25 func Query(conns []Conn, query string) Result { 26 ch := make(chan Result, len(conns)) // buffered 27 for _, conn := range conns { 28 go func(c Conn) { 29 ch <- c.DoQuery(query): 30 }(conn) 31 } 32 return <-ch 33 } 34 35 func XQuery(conns []Conn, query string) Result { 36 ch := make(chan Result, 1) // buffer of 1 item 37 for _, conn := range conns { 38 go func(c Conn) { 39 select { 40 case ch <- c.DoQuery(query): 41 // nothing to do 42 default: // executes if ch is blocked 43 // nothing to do 44 } 45 }(conn) 46 } 47 return <-ch 48 } 49 50 51 func Compose(f, g func(x float) float) 52 func(x float) float { 53 return func(x float) float { 54 return f(g(x)) 55 } 56 } 57 58 print(Compose(sin, cos)(0.5)) 59 60 go func() { // copy input to output 61 for val := range input { 62 output <- val 63 } 64 }()