after.go 780 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. var start time.Time
  7. func init() {
  8. start = time.Now()
  9. }
  10. func service1(c chan string) {
  11. time.Sleep(3 * time.Second)
  12. c <- "Hello from service 1"
  13. }
  14. func service2(c chan string) {
  15. time.Sleep(5 * time.Second)
  16. c <- "Hello from service 2"
  17. }
  18. func main() {
  19. fmt.Println("main() started", time.Since(start))
  20. chan1 := make(chan string)
  21. chan2 := make(chan string)
  22. go service1(chan1)
  23. go service2(chan2)
  24. select {
  25. case res := <-chan1:
  26. fmt.Println("Response from service 1", res, time.Since(start))
  27. case res := <-chan2:
  28. fmt.Println("Response from service 2", res, time.Since(start))
  29. case <-time.After(2 * time.Second):
  30. fmt.Println("No response received", time.Since(start))
  31. }
  32. fmt.Println("main() stopped", time.Since(start))
  33. }