| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package main
- import (
- "fmt"
- "time"
- )
- var start time.Time
- func init() {
- fmt.Println("init...")
- start = time.Now()
- }
- func service1(c chan string) {
- // time.Sleep(3 * time.Second)
- fmt.Println("service1() started", time.Since(start))
- c <- "Hello from service 1"
- }
- func service2(c chan string) {
- // time.Sleep(5 * time.Second)
- fmt.Println("service2() started", time.Since(start))
- c <- "Hello from service 2"
- }
- func main() {
- selectTest()
- /*
- fmt.Println("main() started", time.Since(start))
- chan1 := make(chan string)
- chan2 := make(chan string)
- go service1(chan1)
- go service2(chan2)
- select {
- case res := <-chan1:
- fmt.Println("Response from service 1", res, time.Since(start))
- case res := <-chan2:
- fmt.Println("Response from service 2", res, time.Since(start))
- }
- fmt.Println("main() stopped", time.Since(start))
- */
- }
- func selectTest() {
- fmt.Println("main() started", time.Since(start))
- chan1 := make(chan string)
- chan2 := make(chan string)
- go service1(chan1)
- go service2(chan2)
- // С этой функций в select выполнится один из кейсов
- // Без задержки будет выполнен default
- // time.Sleep(1 * time.Second)
- select {
- case res := <-chan1:
- fmt.Println("Response from service 1", res, time.Since(start))
- case res := <-chan2:
- fmt.Println("Response from service 2", res, time.Since(start))
- // default:
- // fmt.Println("No responce received", time.Since(start))
- }
- fmt.Println("main() stopped", time.Since(start))
- }
|