一个处理信号并退出的例子
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
exit := false
for !exit {
select {
case s := <- c:
if s == syscall.SIGINT {
fmt.Println("caught SIGINT")
exit = true
} else if s == syscall.SIGTERM {
fmt.Println("caught SIGTERM")
exit = true
} else {
fmt.Println("caught unexpect signal: ", s)
}
}
}
}
os.Signalsyscall.SIGINTsyscall.SIGTERM