Standard library / os/signal
os/signal.Notify()
Registers a channel to receive signals without blocking the signal sender. Use buffered channels and call Stop to unregister. Delivery can coalesce or be dropped. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Notify(c chan<- os.Signal, sig ...os.Signal)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| c | chan<- os.Signal | Channel used for signal delivery. |
| sig | ...os.Signal | Signal or list of signals, as indicated by the signature. |
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "os/signal" "syscall" "time")
func must(e error) { if e != nil { panic(e) }}func main() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGUSR1) defer signal.Stop(c) must(syscall.Kill(os.Getpid(), syscall.SIGUSR1)) select { case s := <-c: fmt.Println(s == syscall.SIGUSR1) case <-time.After(time.Second): panic("timeout") }}