-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
36 lines (27 loc) · 759 Bytes
/
main.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
package main
import (
"demo/template"
"fmt"
)
func main() {
// data structure the template will be applied to
vars := make(map[string]interface{})
vars["Name"] = "Brienne"
vars["House"] = "Tarth"
vars["Traits"] = []string{"Brave", "Loyal"}
// process a template string
resultA := template.ProcessString("{{.Name}} of house {{.House}}", vars)
// process a template file
resultB := template.ProcessFile("templates/got.tmpl", vars)
fmt.Println(resultA, "\n")
fmt.Println(resultB)
// using a Struct
type Westerosi struct {
Name string
House string
Traits []string
}
jorah := Westerosi{"Ser Jorah", "Mormont", []string{"Brave", "Protective"}}
resultC := template.ProcessFile("templates/got.tmpl", jorah)
fmt.Println(resultC)
}