I am trying to build a map[string]map[string]string which will look something like this:
{ "notes":
{
"Title":note.Title,
"Body":note.Body,
},
{
"Title":note.Title,
"Body":note.Body,
},
{
"Title":note.Title,
"Body":note.Body,
},
}
from a struct (notes) of structs (note)
I have thought of doing it like this:
for _, note := range notes {
thisNote := map[string]string{
"Title":note.Title,
"Body":note.Body,
}
content["notes"] = append(content["notes"], thisNote)
}
But obviously that is not going to work because I am trying to append a map to a map rather than a slice.
Is there a really easy solution to this that I am missing?
I'm pretty sure you can use a struct like this instead since mustache receives the data as an interface{}
func handler(w http.ResponseWriter, r *http.Request) {
var data struct {
Notes []*Note
}
notes := ...
data.Notes = notes
tmpl := ...
templ.Render(data, w)
}
Thanks to Running Wild for this answer, it was in a comment but I thought I would add it here for anyone trying to do the same thing.
The issue was that I needed to make a map[string][]map[string]string
rather than a map[string]map[string]string