signal.Notity
とGoroutineを使います。
package main
import (
"net/http"
"os"
"os/signal"
)
func handler(w http.ResponseWriter, r *http.Request) {
}
func main() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
for _ = range ch {
signal.Stop(ch)
os.Exit(0)
}
}()
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}