Package Main;import ("OS" "Os/signal" "FMT")//signal packet provides two functions//NOTIFYF () for listening signals//stop () used to stop the Listener Func main () {ch: = Make (Chan os. Signal)//notify for the monitoring signal//parameter 1 for the received signal channel//parameter 2 and the subsequent signal//os to listen. Interrupt indicates interrupt//os. Kill kills the exit process signal. Notify (CH, os. Interrupt, OS. Kill);//Get the signal, if not it will always be blocked here. S: = <-ch;//We kill the process by CTRL + C or with taskkill/pid-t-F to see the effect. Fmt. PRINTLN ("Signal:", s);}
Package Main;import ("OS", "Os/signal" "FMT") func main () {ch: = make (chan os). Signal);//If you do not specify a signal to listen on, the default is all signal Signal. Notify (CH);//stop forwarding the signal to CH, CH will no longer receive any signal signal. Stop (CH);//ch will always be blocked here because it will not receive any signals//so the exit output below cannot perform <-ch;fmt. Println ("Exit");}
Study and use of os/signal package in go language