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 "time"
16 )
17
18
19
20 func ExampleNotifyContext() {
21 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
22 defer stop()
23
24 p, err := os.FindProcess(os.Getpid())
25 if err != nil {
26 log.Fatal(err)
27 }
28
29
30
31
32
33 if err := p.Signal(os.Interrupt); err != nil {
34 log.Fatal(err)
35 }
36
37 select {
38 case <-time.After(time.Second):
39 fmt.Println("missed signal")
40 case <-ctx.Done():
41 fmt.Println(ctx.Err())
42 stop()
43 }
44
45
46
47 }
48
View as plain text