3 ms·
When using Go's stdlib http.Server, there is no need to explicitly w.WriteHeader(http.StatusOK) if one simply uses the Write method on the given ResponseWriter
by jimsmart 8y ago
When using Go's stdlib http.Server, there is no need to explicitly w.WriteHeader(http.StatusOK) if one simply uses the Write method on the given ResponseWriter — 200/Ok is the implicit default that is set if no status has previously been written.
e.g. Instead of:
io.WriteString(w, "Hello, World!" + "\n")
One can do:
w.Write([]byte("Hello, World!\n"))
And then the previous call to w.WriteHeader can be removed.
Additionally, one can also remove the explicit setting of the content type also, and it will be autodetected — although this detection comes with a minor cost, so one may consider this decision to be optional.
See https://godoc.org/net/http#ResponseWriter https://godoc.org/net/http#ResponseWriter for more info.
HTH!