Handle Ctrl+C (Signal Interrupt) Close in the Terminal
·
372 words
·
2 minutes read
When running a Go program in the terminal, your program could receive a signal interrupt from the OS for any number of reasons. One of which is if the user presses Ctrl+C
on their keyboard (or whatever your operating system/terminal is set to). We can execute some code when this interrupt is received, mainly to clean up and reset what we were working on.
In our example we use a goroutine
to listen for the interrupt from signal.Notify()
(part of the signal package) and execute our clean up code when one is sent.
|
|
This will output:
|
|
And here’s what it looks like: