示例:
func tmpl(w http.ResponseWriter, r *http.Request) {
t1, err := template.ParseFiles("test.html")
if err != nil {
log.Fatal(err)
}
t1.Execute(w, "hello world")
}
//test html template
func test1() {
server := http.Server{
Addr: "127.0.0.1:8080",
}
http.HandleFunc("/tmpl", tmpl)
server.ListenAndServe()
}
type Person struct {
Name string
Age int
}
//test
func test2() {
p := Person{Name: "skay", Age: 18}
tmpl, err := template.New("test").Parse("Name: {{.Name}}, Age: {{.Age}}")
if err != nil {
log.Fatal(err)
}
tmpl.Execute(os.Stdout, p)
}
type Friend struct {
Fname string
}
type Human struct {
UserName string
Emails []string
Friends []*Friend
}
func test3() {
f1 := &Friend{Fname: "Tom"}
f2 := &Friend{Fname: "Jack"}
t := template.New("test")
t = template.Must(t.Parse(
`hello {{.UserName}}!
{{ range .Emails }}
an email {{ . }}
{{- end }}
{{ with .Friends }}
{{- range . }}
my friend name is {{.Fname}}
{{- end }}
{{ end }}`))
p := Human{UserName: "Lily", Emails: []string{"ai@163.com", "ai@qq.com"}, Friends: []*Friend{f1, f2}}
t.Execute(os.Stdout, p)
}
func test4() {
tx := template.Must(template.New("hh").Parse(
`{{range $x := . -}}
{{$y := .}}
{{println $x $y}}
{{- end}}`))
s := []int{1, 2, 3, 4, 5}
_ = tx.Execute(os.Stdout, s)
}
func test5() {
tx := template.Must(template.New("hh").Parse(
`{{range $x := . -}}
{{$y := 333}}
{{- if (gt $x 33)}}{{println $x $y ($z := 444)}}{{- end}}
{{- end}}
`))
s := []int{11, 22, 33, 44, 55}
_ = tx.Execute(os.Stdout, s)
}
func test6() {
t1 := template.New("test1")
tmpl, err := t1.Parse(
`
{{- define "T1"}}ONE {{println .}} {{end}}
{{- define "T2"}}{{template "T1" $}} {{end}}
{{- template "T2" . -}}
`)
if err != nil {
log.Fatal(err)
}
tmpl.Execute(os.Stdout, "hello world")
}
//range...end
func test7() {
tx := template.Must(template.New("hh").Parse(
`{{range $key,$value := . -}}
{{println $key $value}}
{{- end}}
`))
tx.Execute(os.Stdout, []int{11, 22, 33, 44})
}
//define template
func test8() {
t1 := template.New("test1")
tmpl, _ := t1.Parse(
`{{- define "T1"}}ONE {{println .}}{{end}}
{{- define "T2"}}TWO {{println .}}{{end}}
{{- define "T3"}}{{template "T1"}}{{template "T2" "haha"}}{{end}}
{{- template "T3" -}}
`)
_ = tmpl.Execute(os.Stdout, "hello world")
}
//test html template
func test9() {
server := http.Server{
Addr: "127.0.0.1:8080",
}
http.HandleFunc("/tmpl2", test9Hanler)
server.ListenAndServe()
}
//define html
func test9Hanler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("t1.html")
if err != nil {
log.Fatal(err)
}
t.Execute(w, "Hello World!")
}
t1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<title>Go Web Programming</title>
</head>
<body>
<div> This is t1.html before</div>
<div>This is the value of the dot in t1.html - [{{ . }}]</div>
<hr />
{{ template "t2.html" }}
<hr />
<div> This is t1.html after</div>
</body>
</html>
{{define "t2.html"}}
<div style="">
This is t2.html<br/>
This is the value of the dot in t2.html - [{{ . }}]
</div>
{{end}}
test.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Go Web</title>
</head>
<body>
{{ . }}
</body>
</html>
以上内容整理自:https://www.cnblogs.com/sunsky303/p/11942030.html