I want to capture the Ctrl+C (SIGINT
) signal sent from the console and print out some partial run totals.
You can use the os/signal http://golang.org/pkg/os/signal/ package to handle incoming signals. Ctrl+C is SIGINT http://en.wikipedia.org/wiki/SIGINT_(POSIX), so you can use this to trap os.Interrupt
.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for sig := range c {
// sig is a ^C, handle it
}
}()
使程序终止并打印信息的方式完全取决于您。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)