How to end a go program from a goroutine


I’m trying to implement some non-typical behavior in a command line interface written in go.

I have a function that runs for a long time, and I want to have a cleanup function that runs when someone ctrl-c’s out of that function.

Here’s a mock-up of the code:

func longRunningFunction() {
    //some set up stuff
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

    go func() {
        fmt.Println("Got an interrupt")
    //the long-running command
    fmt.Println("the end")

In the normal case, using ctrl-c will not be necessary and the function will finish normally. (Because of this, I can’t have something (like a channel) in the main thread that blocks until the cleanup goroutine has finished.) However, in the case that a user does press ctrl-c, I would like to end the program immediately, not printing “the end” (and ideally not finishing the long-running command).

That’s not happening right now. Currently, the command line output looks like this:

//long-running-command's output
^CGot an interrupt
//cleanup code's output
//more of long-running-command's output
the end

I’m confused on a few fronts – why is the program still printing after the prompt returns, and why is “the end” still being printed? How can I avoid this behavior? Is this scenario even possible in go? Thanks!


You’re continuing execution after your signal handler. If you want to exit the process, call os.Exit:

go func() {
    fmt.Println("Got an interrupt")

Answered By – JimB

Answer Checked By – Willingham (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.