2017-01-05 147 views
-2

我從Brian W. Kernighan,Alan Donovan編寫的Go The Programming Language一書中寫了一個任務。它的任務№3.4 我的要求看起來像這樣的處理程序:http.ResponseWriter不設置標題內容類型

func handler(w http.ResponseWriter, r *http.Request) { 
    poly(w) 
    w.Header().Set("ContentType", "image/svg+xml") 
    fmt.Println(w.Header().Get("ContentType")) 
} 

聚(W) - 它是在作家返回SVG文件的功能。 另外,我嘲笑了ContentType的價值,它是「image/svg + xml」。 但是,當我看到在發展鉻(F12)菜單中我看到這一點: network menu in debug

而且着,當然,我看到SVN文件的XML文本,沒有圖像。

所以,我有問題:這是我的錯誤,或者它是golang中的錯誤,或者它是正常的sutiation。

+1

根據給出的答案,這是一個印刷錯誤問題。 – silentsod

回答

3

您必須在寫入響應主體之前設置標題。有關更多詳細信息,請參閱ResponseWriter文檔。

另外,還有一個印刷錯誤。標題名稱是「Content-Type」,而不是「ContentType」

func handler(w http.ResponseWriter, r *http.Request) { 
    w.Header().Set("Content-Type", "image/svg+xml") 
    poly(w) 
}