These steps cover writing and running your application:
- From your terminal/console application, create a new directory called chapter1/templates.
- Navigate to this directory.
- 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!
- 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}}: ...