Part 001 Simple Go Template Usage

Go has a pair of very powerful template libraries that are fun to use and simple. Since I am using it for my static blog generator I have built a bunch of examples that I want to share.

First a really simple use of a template (kind of a template hello world):

Templates start with {{ and end with }}.

There are a bunch of functions and ways to extend the templates. Let's start with the basics. Let's just substitute in a value. Note the . before the word Name. It is important.

Example (ex1.go):

package main

// (C) Philip Schlump, 2013.
// MIT Licensed, see LICENSE.txt

import (
	"os"
	"text/template"
)

type Person struct {
	Name string
}

const templ = `
My name is: {{.Name}}.

`

func main() {
	person := Person{
		Name: "Philip J. Schlump",
	}

	t := template.New("simple-template")
	t, err := t.Parse(templ)
	if err != nil {
		panic(err)
		os.Exit(1)
	}

	err = t.ExecuteTemplate(os.Stdout, "simple-template", person)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
}


To run this you can

$ go run ex1.go


To compile it and run it

$ go build ex1.go
$ ./ex1

The output is (ex1.out):


My name is: Philip J. Schlump.



You can substitute multiple values in a single template.

Example (ex1a.go):

package main

// (C) Philip Schlump, 2013.
// MIT Licensed, see LICENSE.txt

import (
	"os"
	"text/template"
)

type Person struct {
	Name      string
	HomeState string
}

const templ = `
My name is: {{.Name}}.
My home state is: {{.HomeState}}.

`

func main() {
	person := Person{
		Name:      "Philip J. Schlump",
		HomeState: "Wyoming",
	}

	t := template.New("simple-template")
	t, err := t.Parse(templ)
	if err != nil {
		panic(err)
		os.Exit(1)
	}

	err = t.ExecuteTemplate(os.Stdout, "simple-template", person)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
}


One error that I ran into was the failure to export a field in my struct. This is a name starting with a lower case letter.

Example (ex1b.go):

package main

// (C) Philip Schlump, 2013.
// MIT Licensed, see LICENSE.txt

import (
	"os"
	"text/template"
)

type Person struct {
	Name      string
	HomeState string
	oopsy     string
}

const templ = `
My name is: {{.Name}}.
My home state is: {{.HomeState}}.
failed to export oopsy: {{.oopsy}}.

`

func main() {
	person := Person{
		Name:      "Philip J. Schlump",
		HomeState: "Wyoming",
		oopsy:     "Boo Boo",
	}

	t := template.New("simple-template")
	t, err := t.Parse(templ)
	if err != nil {
		panic(err)
		os.Exit(1)
	}

	err = t.ExecuteTemplate(os.Stdout, "simple-template", person)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
}


will produce (ex1b.out):


My name is: Philip J. Schlump.
My home state is: Wyoming.
failed to export oopsy: panic: template: simple-template:4:26: executing "simple-template" at <.oopsy>: oopsy is an unexported field of struct type main.Person

goroutine 1 [running]:
main.main()
	/home/pschlump/www/sketchground/pjs/99-static/res/2013-10-23-Simple-Go-Template-Usage/ex1b.go:36 +0x1bc

goroutine 3 [runnable]:
text/template/parse.lexText(0xc200077000, 0x527a08)
	/usr/local/go/src/pkg/text/template/parse/lex.go:228 +0x361
text/template/parse.(*lexer).run(0xc200077000)
	/usr/local/go/src/pkg/text/template/parse/lex.go:198 +0x3e
created by text/template/parse.lex
	/usr/local/go/src/pkg/text/template/parse/lex.go:191 +0x111
exit status 2


Code tested on: Ubuntu 12.04, Mac 10.8, Windows 7 in go 1.1.2


•       •       •       •       •       •

Summary: # of Words: 313
Author: Philip J. Schlump
Published On: 2013-10-23

Download code from this articles in .tar.gz for Mac/Linux/Unix or .zip with CR/LF for Windows format.

 

Before You Go....

Have you read "Unintend Consinsequences"?

"I laughed so hard it hurt..."
    Rod Brown

"Incredibly funny! Incredibly true!"
    Tad Stevens