Search code examples
gopanic

Go to react to Ctrl + D


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!


Solution

  • 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