In Go I have a map[string]int
. I want to display values from this map through a Template from the text/template
package. If the map doesn’t contain a specific value, I want to display “n/a” instead. This is what I have tried:
{{with index $.MyMap .MyKey}}{{.}}{{else}}n/a{{end}}
Unfortunately this doesn’t work if the map contains a value for my key, but the value is 0. Then “n/a” is also displayed. How can I make sure that “n/a” is only displayed, if the map didn’t contain the requested value?