Go to react to Ctrl + D

2.4k views Asked by At

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

There are 1 answers

0
semicircle21 On BEST ANSWER

With Ctrl-C, the process receives a KILL signal.

You can catch it with os/signal, read the example for the detail:

http://golang.org/pkg/os/signal/#pkg-examples