Skip to content

Instantly share code, notes, and snippets.

@yangyuqian
Created March 10, 2017 02:08
Embed
What would you like to do?
Graceful Exit in Go Program
/*
* Killing a program immediately sometimes leads to corrupted data or files, which brings unexpected result in your system.
* In Go, you can catch the termination signals and let your program decide the time of exiting.
*/
package main
import (
"os"
"os/signal"
"syscall"
)
func main() {
println("Notify the signals ...")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
nsig := <-sigs
println("... Exit with: ", nsig.String())
signal.Stop(sigs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment