I\'m trying to print the values of a map, whose keys have a dot (.
) on it.
Example map:
type TemplateData struct {
Data map[string] int
As @martin-ghallager said, one needs to use an external function to access those elements.
Helpfully, the standard library already provides the index function (which does exactly what Martin's dotNotation
function does).
To use it just write:
{{ index .Data "core.value" }}
The index
function will return a default value in case the key is not present. This works if your dictionary has homogeneous data, however it will return the wrong value when it is heterogeneous. In such a case you can explicitly set the default with:
{{ 0 | or (index .Data "core.value") }}