Tutorial by Examples

Note the use of {{.}} to output the item within the template. package main import ( "fmt" "os" "text/template" ) func main() { const ( letter = `Dear {{.}}, How are you?` ) tmpl, err := template.New("letter").Pa...
Note the use of {{range .}} and {{end}} to cycle over the collection. package main import ( "fmt" "os" "text/template" ) func main() { const ( letter = `Dear {{range .}}{{.}}, {{end}} How are you?` ) tmpl, err := template...
In this example, a function map named funcMap is supplied to the template via the Funcs() method and then invoked inside the template. Here, the function increment() is used to get around the lack of a less than or equal function in the templating language. Note in the output how the final item in t...
Note how field values are obtained using {{.FieldName}}. package main import ( "fmt" "os" "text/template" ) type Person struct { FirstName string LastName string Street string City string State string Zip...
Note the different package import. package main import ( "fmt" "html/template" "os" ) type Person struct { FirstName string LastName string Street string City string State string Zip string Ava...
First, here's what can happen when text/template is used for HTML. Note Harry's FirstName property). package main import ( "fmt" "html/template" "os" ) type Person struct { FirstName string LastName string Street string Cit...

Page 1 of 1