[TOC] > [参考](https://www.twle.cn/t/381) ## demo <details> <summary>main.go</summary> ``` type App struct { quitSign chan os.Signal } func NewApp() *App { return &App{ quitSign: make(chan os.Signal, 2), } } func (this *App) listenQuit() { signal.Notify(this.quitSign, os.Interrupt, syscall.SIGTERM) go func() { <-this.quitSign fmt.Println("\r- Ctrl+C pressed in Terminal") this.close() os.Exit(0) }() } func (this *App) close() { log.Printf("%v", "退出 服务") // code 关闭其他 close //如 //this.tcpListener.Close() //this.clientMap.Close() log.Printf("%v", "退出 success") } func (this *App) Run() { this.listenQuit() for{ log.Printf("%v","1") time.Sleep(2*time.Second) } } func main() { app := NewApp() app.Run() } ``` </details> <br/>