diff --git a/examples/signals/signals.go b/examples/signals/signals.go index 4cd373b..ba587e1 100644 --- a/examples/signals/signals.go +++ b/examples/signals/signals.go @@ -18,29 +18,21 @@ func main() { // Go signal notification works by sending `os.Signal` // values on a channel. We'll create a channel to - // receive these notifications (we'll also make one to - // notify us when the program can exit). + // receive these notifications. + // **Note** that this channel is "buffered". sigs := make(chan os.Signal, 1) - done := make(chan bool, 1) // `signal.Notify` registers the given channel to // receive notifications of the specified signals. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - // This goroutine executes a blocking receive for - // signals. When it gets one it'll print it out - // and then notify the program that it can finish. - go func() { - sig := <-sigs - fmt.Println() - fmt.Println(sig) - done <- true - }() - - // The program will wait here until it gets the - // expected signal (as indicated by the goroutine - // above sending a value on `done`) and then exit. fmt.Println("awaiting signal") - <-done + + // Because the `sigs` channel is buffered, the program + // will wait here until it gets the expected signal and + // then exit. + sig := <-sigs + fmt.Println() + fmt.Println(sig) fmt.Println("exiting") }