Created
May 22, 2013 22:50
-
-
Save macu/5631569 to your computer and use it in GitHub Desktop.
Example of listening for interrupt signal and confirming exit in the shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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