LoginSignup
2
2

More than 5 years have passed since last update.

ListenAndServeをCTRL+Cで止める

Posted at

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)
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2