go 监听 ctrl+c 退出程序信号
代码实现
package main
import (
"log"
"os"
"os/signal"
"syscall"
)
func main() {
//TODO 协程执行业务代码
//监听退出序号
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Println(sig)
done <- true
}()
log.Println("Server Start Awaiting Signal")
<-done
log.Println("Exiting")
}