英文:
Go to react to Ctrl + D
问题
我经常使用Ctrl + d
来关闭正在运行的Go程序,无论是通过go run
还是二进制文件运行的。我该如何让代码对这个意外输入ctrl + d
做出反应呢?例如,我正在运行一个创建EC2
容器的脚本,但我在运行时取消了程序。然后我希望这段代码能对这个ctrl + d
做出反应,并在关闭之前终止EC2
。
请告诉我!谢谢!
英文:
I often use Ctrl + d
to shut down Go programs that is being run by go run
or in binary. How do I make the code to react to this unexpected input ctrl + d
? For example, I am running a script that creates EC2
containers but I cancel the program during run time. Then I want this code to react to this ctrl + d
and terminate the EC2
before it is shut down.
Please let me know! Thanks!
答案1
得分: 2
使用Ctrl-C,进程会接收到一个KILL信号。
你可以使用os/signal来捕获它,详细的示例可以参考这个链接:
http://golang.org/pkg/os/signal/#pkg-examples
英文:
With Ctrl-C, the process receives a KILL signal.
You can catch it with os/signal, read the example for the detail:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论