go servemux
misc
status codes
In general you can lookup an existing http status code constant in
http/status.go
. This might be prefered over writing the number directly.
You can only call w.WriteHeader
once per responce. Any additional calls and go
will log a warning message. Additionally, if you do not call it before the first
call to w.Write
a 200 OK
code will be sent.
auto generated headers
When sending a responce Go will automatically set three system-generated
headers: Date
and Content-Length
and Content-Type
.
The Content-Type
header is particuarly interesting. Go will attempt to set the
correct one for you by content sniffing the responce body with the
http.DetectContentType()
function. If it can't guess the content type, go will
fall back to setting Content-Type: application/octec-stream
instead. Notable,
this happens by default with json. You can of course manually correct this:
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"name":"Alex"}`))