如提示所示,这是因为您尚未设置内容类型。引用自
http.ResponseWriter:
// Write writes the data to the connection as part of an HTTP reply.// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)// before writing the data. If the Header does not contain a// Content-Type line, Write adds a Content-Type set to the result of passing// the initial 512 bytes of written data to DetectContentType.Write([]byte) (int, error)
如果您自己未设置内容类型,则第一个调用
ResponseWriter.Write()将调用
http.DetectContentType()以猜测要设置的内容。如果您发送的内容以开头
"<form>",则不会将其检测为HTML,而是
"text/plain;charset=utf-8"会进行设置(“指示”浏览器将内容显示为文本,而不尝试将其解释为HTML)。
"<html>"例如,如果内容以开头,则内容类型
"text/html; charset=utf-8"将自动设置,并且无需进一步操作即可工作。
但是,如果您知道要发送的内容,请不要依赖自动检测,而且,自己设置它比在其上运行检测算法要快得多,因此只需在写入/发送任何数据之前添加以下行即可:
w.Header().Set("Content-Type", "text/html; charset=utf-8")并使
post.html模板成为完整的有效HTML文档。
Also another piece of advice: in your pre you religiously omit checking
returned errors. Don’t do that. The least you could do is print them on the
console. You will save a lot of time for yourself if you don’t omit errors.



