我有一个程序,我在其中生成一个goroutine来监视SIGINT,这样我就可以在退出程序之前做一些清理工作。但是我注意到在Ctrl + C之后立即返回终端的cmd提示符,这意味着一堆输出溢出。这是代码和我所看到的一个例子:
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
//spawn a thread to wait on the sigterm op
go func() {
<-sigs
fmt.Println("Received a signal interrupt, stopping container")
stopContainer(containerName)
os.Exit(0)
}()
runtime.Gosched()
//rest of program that prints logs from a running docker container
输出看起来像这样
^CReceived a signal interrupt, stopping container
Received a signal interrupt, stopping container
mbp:myDir myName$ Stopping the 'javaTest-tools' container...
Stopping the 'javaTest-tools' container...
Build time: 3m31.275495945s
Done.
您可以看到以mpb:myDir myName$
开头的第3行输出是返回的cmd行提示,然后是stdout。我有点困惑的是为什么cmd行的行为就像程序结束时那样早。