Post

Go template

Working with Go templates including ParseGlob, ExecuteTemplate, passing structs and slices to templates, and rendering nested data structures.

The first example shows the basics of Go template initialization using template.ParseGlob and executing a named template with ExecuteTemplate.

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
package main

import (
	"os"
	// "io"
	"fmt"
	"log"
	// "strings"
	"text/template"
)

var tpl *template.Template

func init() {
	fmt.Println("Initializing ...")
	tpl = template.Must(template.ParseGlob("*.gohtmlx"))
}

func main()  {
	// strongly typed channel
	// err := tpl.Execute(os.Stdout, nil)
	// if err != nil {
	// 	log.Fatalln(err)
	// }

	err := tpl.ExecuteTemplate(os.Stdout, "3.gohtml", nil)
	if err != nil {
		log.Fatalln(err)
	}
}

More advanced examples

This more advanced example defines multiple structs (kernelMember, car, item) and passes slices, maps, and nested data structures to various Go templates. It demonstrates how to iterate over data in templates using different .gohtml files.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main

import (
	"os"
	// "io"
	"fmt"
	"log"
	// "strings"
	"text/template"
)

var tpl *template.Template

func init() {
	fmt.Println("Initializing ...")
	tpl = template.Must(template.ParseGlob("*.gohtml"))
}

type kernelMember struct {
	Name string
	Position int
}

type car struct {
	Manufacturer string
	Model        string
	Doors        int
}

// I am gonna define a "struct" consuming the previous two

type item struct {
	People    []kernelMember
	Transport []car
}

func main()  {

	j := kernelMember{
		Name: "Jan Toth",
		Position: 1,
	}
	v := kernelMember{
		Name: "Vilko",
		Position: 7,
	}
	ja := kernelMember{
		Name: "Jaroslav",
		Position: 3,
	}
	p := kernelMember{
		Name: "Peto",
		Position: 1,
	}
	k := kernelMember{
		Name: "Krissko",
		Position: 1,
	}

	m := car{
		Manufacturer: "Mercedes",
		Model: "Class E",
		Doors: 4,
	}

	f := car{
		Manufacturer: "Ferrari",
		Model: "Lancer",
		Doors: 3,
	}

	// I am going to put this struct "jano" into slice of list

	sliceOfKernelMembers := []kernelMember{j, v, ja, p, k}
	sliceOfCars := []car{m, f}

	data := item{
		People: sliceOfKernelMembers,
		Transport: sliceOfCars,
	}

	err := tpl.ExecuteTemplate(os.Stdout, "tpl.gohtml", sliceOfKernelMembers)
	if err != nil {
		log.Fatalln(err)
	}

	// tpl-neasted.gohtml
	err = tpl.ExecuteTemplate(os.Stdout, "tpl-neasted.gohtml", data)
	if err != nil {
		log.Fatalln(err)
	}

	sliceVariable := []string{"Gandhi", "Buddha", "Jesus"}

	err = tpl.ExecuteTemplate(os.Stdout, "slice.gohtml", sliceVariable)
	if err != nil {
		log.Fatalln(err)
	}

	err = tpl.ExecuteTemplate(os.Stdout, "slice-with-index.gohtml", sliceVariable)
	if err != nil {
		log.Fatalln(err)
	}

	// maps

	mapVariable := map[string]string{
		"India": "Ghandi",
		"America": "MArtin L. King",
		"Love": "Jesus",
	}

	err = tpl.ExecuteTemplate(os.Stdout, "map.gohtml", mapVariable)
	if err != nil {
		log.Fatalln(err)
	}

}

This post is licensed under CC BY 4.0 by the author.