The GO programming language has a nice and useful standard library, which includes a powerful templating engine out of the box.
Here I wrote an example, generating HTML output from a simple data structure.
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
| package main
import (
"html/template"
"log"
"os"
)
func main() {
page := `
<!DOCTYPE html>
<html><head><title>my todo list</title></head>
<body><h1>my TODO list</h1>
<ul>
{{ range $item := . }}
<li> {{ $item.Priority }} {{ $item.Topic }} </li>
{{ end }}
</ul>
</body></html>
`
type Todo struct {
Priority int
Topic string
}
var todos = []Todo{
{1, "Take out the dog"},
{2, "Feed the cat"},
{3, "Learn GO programming"},
}
t := template.Must(template.New("page").Parse(page))
err := t.Execute(os.Stdout, todos)
if err != nil {
log.Println("executing template:", err)
}
}
|
This program generates the following HTML output:
<!DOCTYPE html>
<html><head><title>my todo list</title></head>
<body><h1>my TODO list</h1>
<ul>
<li> 1 Take out the dog </li>
<li> 2 Feed the cat </li>
<li> 3 Learn GO programming </li>
</ul>
</body></html>
Next step: use the template to provide interactive web pages …