英文:
What is the difference between tmp and html in Go?
问题
我最近正在学习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
提前谢谢。
英文:
Im recently learning Golang and Im trying to create a website with Go as backend.
I done it correctly with a framework called Martini
but I want to do it without frameworks.
Can anyone tell me whats the difference between html and tmpl? Because I want to call a page that loads a table with DB rows and I guess that first I have to understand what is the difference.
This is what I tried:
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")
}
I dont know what is the structure but I put the index.html on : /templates/index.html:
Contains Hello World
Thank You in Advance.
答案1
得分: 2
没有。将文件命名为index.html
或index.tmpl
取决于作者。我个人更喜欢.tmpl
,因为这些文件包含的不仅仅是HTML。
一些特定的包(比如martini-render)可能只会查找特定的文件扩展名,但几乎所有的包都是可配置的。
如果你刚刚开始学习,我建议阅读http://jan.newmarch.name/golang/template/chapter-template.html
英文:
None. Calling a file index.html
or index.tmpl
is up to the author. I personally prefer .tmpl
as the files contain more than just HTML.
Some specific packages (like martini-render) might look only for certain file extensions, but nearly all should be configurable.
If you are just starting out I suggest reading http://jan.newmarch.name/golang/template/chapter-template.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论