How to do it...

These steps cover writing and running your application:

  1. From your terminal/console application, create a new directory called chapter1/templates.
  2. Navigate to this directory.
  3. Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter1/templates, or use this as an exercise to write some of your own!
  4. Create a file called templates.go with the following contents:
        package templates                import (                "os"                "strings"                "text/template"        )                const sampleTemplate = `                This template demonstrates printing a {{ .Variable |                 printf "%#v" }}.                         {{if .Condition}}                If condition is set, we'll print this                {{else}}                Otherwise, we'll print this instead                {{end}}                        Next we'll iterate over an array of strings:                {{range $index, $item := .Items}} {{$index}}: ...

Get Go Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.