2016-10-17 17 views

cevap

56

http.ResponseWriter.WriteHeader'u kullanın. Belge:

WriteHeader, durum kodu içeren bir HTTP yanıt üstbilgisi gönderir. WriteHeader açıkça çağrılmıyorsa, Write'a yapılan ilk çağrı örtülü bir WriteHeader'ı (http.StatusOK) tetikler. Böylece, WriteHeader'a yapılan açık çağrılar çoğunlukla hata kodlarını göndermek için kullanılır.

Örnek:

func ServeHTTP(w http.ResponseWriter, r *http.Request) { 
    w.WriteHeader(http.StatusInternalServerError) 
    w.Write([]byte("500 - Something bad happened!")) 
} 
8
w.WriteHeader(http.StatusInternalServerError) 
w.WriteHeader(http.StatusForbidden) 

tam listesini WriteHeader(int) Apart here

27

örneğin, yardımcı yöntemi http.Error kullanabilirsiniz:

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) 
} 

http.Error() and http.StatusText() metodları arkadaşlarınız mı

İlgili konular