Skip to content

Instantly share code, notes, and snippets.

@macu
Created May 22, 2013 22:50
Show Gist options
  • Select an option

  • Save macu/5631569 to your computer and use it in GitHub Desktop.

Select an option

Save macu/5631569 to your computer and use it in GitHub Desktop.
Example of listening for interrupt signal and confirming exit in the shell
package main
import (
"fmt"
"os"
"os/signal"
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for {
<-c // await Ctrl+C
fmt.Println("\nExiting...")
ConfirmExit:
for {
fmt.Print("Save changes [y/n/Cancel]? ")
var a string
fmt.Scanln(&a)
switch {
case len(a) == 0, a[0] == 'c', a[0] == 'C':
fmt.Println("Cancelled.")
break ConfirmExit
case a[0] == 'y', a[0] == 'Y':
// TODO save stuff
fmt.Println("Saved.")
os.Exit(0)
case a[0] == 'n', a[0] == 'N':
fmt.Println("Not saved.")
os.Exit(0)
default:
fmt.Println("Huh?")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment