This is a creation in Article, where the information may have evolved or changed.
Parsing of templates
Since it's a website,
fmt.Fprintf"Hello world, this is my first page!")
This way must not be used, you have to parse the template.
The package used for template parsing is "Html/template", the pilot package, and then the index function is overwritten:
//先导入html/template包import"html/template"func Index(w http.ResponseWriter, r *http.Request) { //解析指定模板文件index.html t, _ := template.ParseFiles("index.html") //输出到浏览器 nil)}
In preparing an HTML file, as follows:
<HTML> <head> <title> 这是一个测试文件 </title> </head> <body> <p> Hello world, this is my first page! </p> </body></HTML>
Run it and get the same results as above.
Then insert the data into the template, which is easy:
func Index(w http.ResponseWriter, r *http.Request) { //用于保存数据的map make(map[string]string) t, _ := template.ParseFiles("index.html") data["Name""BCL" t.Execute(w, data)}
Insert a new tag in the HTML file:
<p> Hello , {{.Name}} </p>
So you can parse the template properly.
Let's take a quick look at the syntax of the Go language template
To be continued .....