I think Calling https://golang.org/pkg/net/http/#Server.SetKeepAlivesEnabled given false should work what you want. (You would need to use https://golang.org/pkg/net/http/#Server instead of using ServeMux directly)
2017年1月17日火曜日 12時36分27秒 UTC+9 Tony Grosinger: > > I would like to create an HTTP server which forces the connection to be > closed after writing the response. > For example: > > func closingHandler(w http.ResponseWriter, r *http.Request) { > // Respond before hijacking? > fmt.Fprintf(w, "Hello World") > > hj, ok := w.(http.Hijacker) > if !ok { > log.Println("Unable to create hijacker") > return > } > > conn, bufrw, err := hj.Hijack() > if err != nil { > log.Println("Unable to hijack request") > return > } > > bufrw.Flush() > conn.Close() > } > func main() { > mux := http.NewServeMux() > mux.HandleFunc("/", closingHandler) > > go func() { > err := http.ListenAndServe(":8080", mux) > if err != nil { > log.Println("Failed to run server" > } > }() > } > > This example does not work however because the client issuing the request > cannot successfully read the response. Is there another way to close the > connection manually that would allow me to respond to the HTTP request > first? > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.