Source file
src/os/signal/example_unix_test.go
1
2
3
4
5
6
7 package signal_test
8
9 import (
10 "context"
11 "fmt"
12 "log"
13 "os"
14 "os/signal"
15 )
16
17 var neverReady = make(chan struct{})
18
19
20
21 func ExampleNotifyContext() {
22 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
23 defer stop()
24
25 p, err := os.FindProcess(os.Getpid())
26 if err != nil {
27 log.Fatal(err)
28 }
29
30
31
32
33
34 if err := p.Signal(os.Interrupt); err != nil {
35 log.Fatal(err)
36 }
37
38 select {
39 case <-neverReady:
40 fmt.Println("ready")
41 case <-ctx.Done():
42 fmt.Println(ctx.Err())
43 stop()
44 }
45
46
47
48 }
49
View as plain text