1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
不能寫文本然后調(diào)用ServeFile在文本后面輸出一個(gè)二進(jìn)制圖片。
如果你想為文字與圖像,然后使用HTML,設(shè)置一個(gè)靜態(tài)文件處理程序,并用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)
}
- 1 回答
- 0 關(guān)注
- 231 瀏覽
添加回答
舉報(bào)