You can use defer:

defer bufrw.Flush()
defer conn.Close()

It will close after method finishs, but I do not know if it will work for 
you because you using go routines to start server, I never see this, I 
always use:

log.Fatal(http.ListenAndServe(":8080", mux))


Well, I think you can try.


Em segunda-feira, 16 de janeiro de 2017 23:36:27 UTC-4, Tony Grosinger 
escreveu:
>
> 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.

Reply via email to