Go handles signals by registering handlers via the os/signal package, which intercepts OS signals like syscall.SIGINT and syscall.SIGTERM and sends them to a Go channel for safe, concurrent processing. Use signal.Notify to direct specific signals to a channel and signal.Reset to stop listening.
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
fmt.Println("Caught signal, shutting down...")
done <- true
}()
<-done
os.Exit(0)
}