Risposte:
Usa http.ResponseWriter.WriteHeader. Dalla documentazione:
WriteHeader invia un'intestazione di risposta HTTP con il codice di stato. Se WriteHeader non viene chiamato in modo esplicito, la prima chiamata a Write attiverà un WriteHeader implicito (http.StatusOK). Pertanto le chiamate esplicite a WriteHeader vengono utilizzate principalmente per inviare codici di errore.
Esempio:
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something bad happened!"))
}
Oltre WriteHeader(int)a poter utilizzare il metodo di supporto http.Error , ad esempio:
func yourFuncHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "my own error message", http.StatusForbidden)
// or using the default message error
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
I metodi http.Error () e http.StatusText () sono tuoi amici
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusForbidden)
elenco completo qui
http: superfluous response.WriteHeader call