Is it possible to catch a sigterm
in Golang and move on on the code, like a panic/defer?
Example:
func main() {
fmt.Println("app started")
setupGracefulShutdown()
for {
}
close()
}
func close() {
fmt.Println("infinite loop stopped and got here")
}
func setupGracefulShutdown() {
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
fmt.Println(" got interrupt signal: ", <-sigChan)
}()
}
// "app started"
// CTRL + C
// ^C "got interrupt signal: interrupt"
// app don't stop
What I want is to print infinite loop stopped and got here
and finish application.
// "app started"
// CTRL + C
// ^C "got interrupt signal: interrupt"
// "infinite loop stopped and got here"
sigChan
and "break" once a value is received from it. What's wrong with that? – Hamrick