golang中的模板没有被渲染
问题描述:
我在一个Golang项目中工作,目前正在尝试渲染一些模板,但模板的HTML代码正在浏览器中打印。我的项目结构是:golang中的模板没有被渲染
|-app
|---models
|---controller
|---templates
|------index.gohtml
|-main.go
然后,我有一个控制器的代码,呈现模板是:
func Index(w http.ResponseWriter, req *http.Request, ps httprouter.Params){
var tpl *template.Template
tpl = template.Must(template.ParseGlob("app/templates/*.gohtml"))
err := tpl.ExecuteTemplate(w, "index.gohtml", nil)
if(err!=nil){
fmt.Println("error:",err.Error())
}
但是我没有得到任何错误。但在浏览器中,我有index.gohtml
内部的代码正在打印,而不是渲染 }
答
结帐net/http/sniff.go。它显示了net/http如何推断MIME类型。
对于HTML内容可以开始:
<!DOCTYPE HTML
<HTML
<HEAD
<SCRIPT
<IFRAME
<H1
<DIV
<FONT
<TABLE
<A
<STYLE
<TITLE
<B
<BODY
<BR
<P
<!--
如果更换用'应用程序/模板/ index.gohtml''index.gohtml'什么? – Albert221
@ Albert221同样,起初我是那样的,但是我得到了相同的结果 –
我只用过'template.ParseFiles',但它应该可以工作。如果它是打印的HTML,那么我想它是解析。奇怪的 – RayfenWindspear