Catching and handling signals
Signals are a useful way for the user or the OS to kill your running application. Sometimes, it makes sense to handle these signals in a more graceful way than the default behavior. Go provides a mechanism to catch and handle signals. In this recipe, we'll explore the handling of signals through the use of a signal handling the Go routine.
Getting ready
Refer to the Getting ready section's steps in the Using command-line flags recipe.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create a new directory called
chapter2/signals
, and navigate to that directory. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter2/signals, or use this as an exercise to write some of your own code!
- Create a file called
signals.go
with the following contents:
package main import ( "fmt" "os" "os/signal" "syscall" ) // CatchSig...