Skip to content

Instantly share code, notes, and snippets.

@yangyuqian
Created March 10, 2017 02:08
Show Gist options
  • Save yangyuqian/2be646614bbc0b224a17a99d96006d64 to your computer and use it in GitHub Desktop.
Save yangyuqian/2be646614bbc0b224a17a99d96006d64 to your computer and use it in GitHub Desktop.
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