Golang os/signal.Stop() and Notify() functions example
package os/signal
Golang os/signal.Stop() and Notify() functions usage example
log.Println("Server started. Press Ctrl-C to stop server")
// Catch the Ctrl-C and SIGTERM from kill command
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
signalType := <-ch
signal.Stop(ch)
log.Println("Exit command received. Exiting...")
// this is a good place to flush everything to disk
// before terminating.
log.Println("Signal type : ", signalType)
os.Exit(0)
}()
See full example at : How to intercept Ctrl-C interrupt or kill signal and determine the signal type tutorial.
References :
Advertisement
Something interesting
Tutorials
+15k Golang : Get checkbox or extract multipart form data value example
+4.7k Golang : Display packages names during compilation
+10.4k PHP : Convert(cast) bigInt to string
+39.2k Golang : UDP client server read write example
+12.6k Golang : List objects in AWS S3 bucket
+8.5k Golang : Go as a script or running go with shebang/hashbang style
+21.9k Golang : Convert Unix timestamp to UTC timestamp
+11.7k Golang : Get month name from date example
+25.1k Golang : How to read integer value from standard input ?
+16.7k Golang : Check if IP address is version 4 or 6
+19k Golang : Close channel after ticker stopped example
+5.4k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag