Part 002 Using A Map Of Strings With A Go Template

You can use templates with a map of strings. This allows a lot of flexibility for supplying the data. JSON can be read into a map like this.

An example (ex2.go):

package main

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

import (

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


func main() {
	person := make(map[string]string, 1)
	person["Name"] = "Philip J. Schlump"

	t := template.New("simple-template")
	t, err := t.Parse(templ)
	if err != nil {

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

To run this you can

$ go run ex2.go

To compile it and run it

$ go build ex2.go
$ ./ex2

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

•       •       •       •       •       •

Summary: # of Words: 115
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