1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| package main
import (
"errors"
"fmt"
"time"
)
func main() {
chErr := make(chan error)
signal := make(chan bool)
select {
case err := <-chErr:
fmt.Println("found error:", err)
default:
fmt.Println("no found error")
}
go func() {
time.Sleep(2 * time.Second)
signal <- true
close(signal)
}()
go func() {
time.Sleep(1 * time.Second)
chErr <- fmt.Errorf("%w", errors.New("can't continue"))
close(chErr)
}()
select {
case err := <-chErr:
fmt.Println("found error:", err)
break
case sig := <-signal:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}
}
|