I have the most basic net/http program that I'm using to learn the namespace in Go:
package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Println(r.URL) go HandleIndex(w, r) }) fmt.Println("Starting Server...") log.Fatal(http.ListenAndServe(":5678", nil)) } func HandleIndex(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Write([]byte("Hello, World!")) }
When I run the program and connect to localhost:5678
in Chrome, I get this in the console:
Starting Server... / 2015/01/15 13:41:29 http: multiple response.WriteHeader calls /favicon.ico 2015/01/15 13:41:29 http: multiple response.WriteHeader calls
But I don't see how that's possible. I print the URL, start up a new goroutine, write the header once, and give it a static body of Hello, World!
It seems like one of two things is happening. Either something behind the scenes is writing another header or somehow HandleIndex
is called twice for the same request. What can I do to stop writing multiple headers?
EDIT: It seems to have something to do with the go HandleIndex(w, r)
line because if I remove go
and just make it a function call instead of a goroutine, I don't get any issues and the browser gets it's data. With it being a goroutine, I get the multiple WriteHeader error and the browser doesn't show "Hello World." Why is making this a goroutine breaking it?
Take a look at the anonymous function you register as the handler of incoming requests:
func(w http.ResponseWriter, r *http.Request) { fmt.Println(r.URL) go HandleIndex(w, r) }
It prints the URL (to the standard output) then calls HandleIndex()
in a new goroutine and continues execution.
If you have a handler function where you do not set the response status before the first call to Write
, Go will automatically set the response status to 200 (HTTP OK). If the handler function does not write anything to the response (and does not set the response status and completes normally), that is also treated as a successful handling of the request and the response status 200 will be sent back. Your anonymous function does not set it, it does not even write anything to the response. So Go will do just that: set the response status to 200 HTTP OK.
Note that handling each request runs in its own goroutine.
So if you call HandleIndex
in a new goroutine, your original anonymous function will continue: it will end and so the response header will be set - meanwhile (concurrently) your started new goroutine will also set the response header - hence the "multiple response.WriteHeader calls"
error.
If you remove the "go"
, your HandleIndex
function will set the response header in the same goroutine before your handler function returns, and the "net/http" will know about this and will not try to set the response header again, so the error you experienced will not happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With