我从 Brian W. Kernighan 和 Alan Donovan 所著的 The Go Programming Language 一书中编写了任务。是任务?3.4 我的请求处理程序如下所示:
func handler(w http.ResponseWriter, r *http.Request) {
poly(w)
w.Header().Set("ContentType", "image/svg+xml")
fmt.Println(w.Header().Get("ContentType"))
}
Run Code Online (Sandbox Code Playgroud)
poly(w) - 它是在 Writer 中返回 svg 文件的函数。另外,我检查了 ContentType 的值,它是“image/svg+xml”。但是当我在 chrome(F12) 中查看开发菜单时,我看到了这个: 调试中的网络菜单
而且,当然,我看到的是 svn 文件的 xml 文本,而不是图片。
所以,我有一个问题:这是我的错误,或者是 golang 中的错误,或者是正常情况。
您必须在写入响应正文之前设置标头。有关更多详细信息,请参阅ResponseWriter文档。
此外,还有一个印刷错误。标题名称是“Content-Type”,而不是“ContentType”
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/svg+xml")
poly(w)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1867 次 |
| 最近记录: |