I'm curious to see if there is a way to marshal a map or struct from a string and a Go Template (the reverse of template.Execute
)
For example, let's say we have the following template for a filename pattern
const NameTemplate = `{{ .Os.Type }}/{{ .Os.Variant }}!{{ .Os.Version }}!{{ .Name }}!{{ .Version }}`
And let's say we have the following map (using a structure in my solution, but I'll use a map here for simplicity)
data := map[string]interface{}{
"Name": "some-name",
"Version": "1.0",
"Os": map[string]interface{}{
"Type": "linux",
"Variant": "debian",
"Version": "9.0",
},
}
Doing a tmpl.Execute(NameTemplate, data)
will result in linux/debian!9.0!some-name!1.0
Now what if we had that string linux/debian!9.0!some-name!1.0
and the template defined above NameTemplate
. Is there a simple way to do obtain a map (or struct) like the one defined in data
?