使用kostix答案,我构建了以下代码(现已适用于Go1)以捕获中断信号并在退出之前进行一些操作:go func() { sigchan := make(chan os.Signal) signal.Notify(sigchan, os.Interrupt) <-sigchan log.Println("Program killed !") // do last actions and wait for all write operations to end os.Exit(0)}()// start main program tasks