1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 // This example passes a context with a signal to tell a blocking function that
19 // it should abandon its work after a signal is received.
20 func ExampleNotifyContext() {
21 ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
24 p, err := os.FindProcess(os.Getpid())
29 // On a Unix-like system, pressing Ctrl+C on a keyboard sends a
30 // SIGINT signal to the process of the program in execution.
32 // This example simulates that by sending a SIGINT signal to itself.
33 if err := p.Signal(os.Interrupt); err != nil {
38 case <-time.After(time.Second):
39 fmt.Println("missed signal")
41 fmt.Println(ctx.Err()) // prints "context canceled"
42 stop() // stop receiving signal notifications as soon as possible.