Handle Ctrl+C (Signal Interrupt) Close in the Terminal

· 307 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() and execute our clean up code when one is sent.

package main

import (

const FILE_NAME = "go-example.txt"

func main() {

    // Setup our Ctrl+C handler

    // Run our program... We create a file to clean up then sleep
    for {
        fmt.Println("- Sleeping")
        time.Sleep(10 * time.Second)

// SetupCloseHandler creates a 'listener' on a new goroutine which will notify the 
// program if it receives an interrupt from the OS. We then handle this by calling 
// our clean up procedure and exiting the program.
func SetupCloseHandler() {
    c := make(chan os.Signal, 2)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    go func() {
        fmt.Println("\r- Ctrl+C pressed in Terminal")

// Used to simulate a 'clean up' function to run on shutdown. Because it's
// just an example it doesn't have any error handling.
func DeleteFiles() {
    fmt.Println("- Run Clean Up - Delete Our Example File")
    _ = os.Remove(FILE_NAME)
    fmt.Println("- Good bye!")

// Create a file so we have something to clean up when we close our program.
func CreateFile() {
    fmt.Println("- Create Our Example File")
    file, _ := os.Create(FILE_NAME)
    defer file.Close()

This will output:

$ go run sigint.go 
- Create Our Example File
- Sleeping
- Ctrl+C pressed in Terminal
- Run Clean Up - Delete Our Example File
- Good bye!

And here's what it looks like:

Example of using Ctrl+C to Exit

Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!