os/signal

Go's standard library package for receiving operating system signals, such as SIGTERM, on a channel instead of being killed by them.

In more detail

signal.Notify(ch, syscall.SIGTERM) delivers the signal to ch; give the channel a buffer of one so no signal is dropped. signal.NotifyContext returns a context that is cancelled when the signal arrives, which fits neatly with shutdown code.

Example

stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM)

Related terms

Taught in

References