I am feeding data from an external json source (over which I have no control) into a template. But some of the names have dot’s in them. So I end up with a template
variable {{.Map.first.name}} where “first.name” is a key in Map. How do I escape the dot? Or is there
some other trick? I’ve tried {{.Map.“first.name”}} but that produces a panic (bad character ‘"’).
Here is a sample program that illustrates the problem.
Many thanks
package main
import (
"os"
"text/template"
"time"
)
const atemplate = `Look at this:
{{range .}} {{.Name}} {{.Count }}
{{.JoinDate}} {{.JoinString}} {{.Map.post.code}}
{{end}}`
type Record struct {
Name string
Count int
JoinDate time.Time
JoinString string
Map map[string]string
}
func main() {
tmpl, err := template.New("test").Parse(atemplate)
if err != nil {
panic(err)
}
var r []Record
r = append(r, Record{
Name: "Suzanne",
Count: 236,
JoinDate: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.Local),
JoinString: "rs",
Map: map[string]string{"key": "value", "post.code": "5068"},
})
err = tmpl.Execute(os.Stdout, r)
if err != nil {
panic(err)
}
}