我最近学习了Golang,而我正在尝试创建一个以Go为后端的网站。我使用称为的框架正确完成了此操作,Martini
但我想不使用框架。
谁能告诉我html和tmpl有什么区别?因为我想调用一个加载带有数据库行的表的页面,所以我想首先我必须了解两者之间的区别。
这是我尝试的:
server.go:
package main
import (
"io/ioutil"
"net/http"
"html/template"
)
func main(){
http.HandleFunc("/index/"), viewIndex)
http.ListenAndServe(":8080", nil)
}
func viewIndex(w http.ResponseWriter, r *http.Request){
t, _ := template.ParseFiles("index.html")
}
我不知道结构是什么,但是我将index.html放在:/templates/index.html:
包含 Hello World
先感谢您。
没有。调用文件index.html
还是index.tmpl
由作者决定。我个人更喜欢.tmpl
文件,因为它们不仅包含HTML。
一些特定的程序包(如martini-render)可能仅查找某些文件扩展名,但几乎所有文件都应是可配置的。
如果您刚刚起步,建议阅读http://jan.newmarch.name/golang/template/chapter-template.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句