[英]Go multiple response.WriteHeader calls for Fprint
我想先打印出短信,然后在文字下方顯示圖像。 但是我得到了http: multiple response.WriteHeader calls
錯誤。
如何在一個頁面中使用一個hadler來提供iamges和文本?
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world!")
fp := path.Join("images", "gopher.png")
http.ServeFile(w, r, fp)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
您不能編寫文本,然后調用ServeFile在文本之后輸出二進制圖片。
如果要在圖像中提供文本,請使用html,設置靜態文件處理程序並使用html:
var tmpl = `<!doctype html>
<html>
<head>
<title>%s</title>
</head>
<body>
<h1>%s</h1>
<div><img src="images/%s"></div>
</body>
</html>
`
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, tmpl, "Hello, world!", "Hello, world!", "gopher.png")
}
func main() {
http.HandleFunc("/", handler)
http.Handle("/images/", http.FileServer(http.Dir("images/")))
http.ListenAndServe(":3000", nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.