For database programs, we need to close the database if users press Ctrl+C
to terminate the program. This post shows how to capture Ctrl+C event and run
the handler in Go.
// http://stackoverflow.com/questions/11268943/golang-is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in// https://gobyexample.com/signalspackagemainimport("os""os/signal""syscall""fmt")funchandleCtrlC(cchanos.Signal){sig:=<-c// handle ctrl+c event here// for example, close databasefmt.Println("\nsignal: ",sig)os.Exit(0)}funcmain(){c:=make(chanos.Signal)signal.Notify(c,os.Interrupt,syscall.SIGTERM)gohandleCtrlC(c)// block herefor{select{}}}