-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-demo.go
70 lines (67 loc) · 1.09 KB
/
template-demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"html/template"
"os"
)
type Friend struct {
Fname string
}
type Person struct {
Username string
Emails []string
Friends []*Friend
Single bool
}
func demoSingeField(){
t := template.New("fieldName example")
t, _ = t.Parse("hello, {{.Username}}")
p := Person{
Username : "bcshuai",
}
t.Execute(os.Stdout, p)
}
func demoLoop(){
t := template.New("demo loop")
t, _ = t.Parse(`
hello {{.Username}}!
{{range .Emails}}
an email {{.}}
{{end}}
{{with .Friends}}
{{range .}}
friend {{.Fname}}
{{end}}
{{end}}
`)
fs := []*Friend{
&Friend{Fname: "TOM"},
&Friend{Fname: "Lily"},
&Friend{Fname: "Jack"},
}
p := Person{
Username : "Mike",
Emails: []string{"123@test.com", "456@test.com"},
Friends: fs,
}
t.Execute(os.Stdout, p);
}
func demoCondition(){
t := template.New("demo condition")
t, _ = t.Parse(`
hello {{.Username}}
{{if .Single}}
I'm Single
{{else}}
I have getten married
{{end}}
`)
p := Person{
Username : "Tome",
Single : false,
}
t.Execute(os.Stdout, p)
}
func main(){
//demoLoop()
demoCondition()
}