I'm writing a simple Go program to display an HTML table of deployed service versions per environment. My program contains the following structs:
type versionKey struct {
Environment string
Service string
}
type templateData struct {
Environments []string
Services []string
Versions map[versionKey]string
}
As you can see, the Versions
map uses a versionKey
as a key for a string value e.g. "1.0.0"
.
I'm passing the templateData
struct to an HTML template and ranging over its Environments
and Services
slices to build the HTML table. The problem is that I need to construct a versionKey
for any given intersection of environment and service so I can use it to look up the version from the Versions
map and output that value in the table cell.
Within the template I have $environment
and $service
variables available from the ranges, but I can't work out the Go template syntax to create the versionKey
struct.
Here's the template code with the markup omitted:
{{$environments := .Environments}}
{{$services := .Services}}
{{$versions := .Versions}}
{{range $service := $services}}
...
{{range $environment := $environments}}
...
{{index $versions ...? }} // How to create versionKey struct map key here?
...
{{end}}
...
{{end}}