Go template comparison operators on missing map key

Use the index function:

{{if eq (index .MyMap "KeyThatDoesntExist") "mystring"}}
  {{.}}
{{end}}

playground example

The index function returns the zero value for the map value type when the key is not in the map. The zero value for the map in the question is the empty string.


You can first check if the key is in the map, and only perform the comparison if it is. You can check with another {{if}} action or with the {{with}} action which also sets the pipeline.

Using {{with}}:

{{with .MyMap.KeyThatDoesntExist}}{{if eq . "mystring"}}Match{{end}}{{end}}

Using another {{if}}:

{{if .MyMap.KeyThatDoesntExist}}
    {{if eq .MyMap.KeyThatDoesntExist "mystring"}}Match{{end}}{{end}}

Note that you can add {{else}} branches to cover other cases. Full coverage with {{with}}:

{{with .MyMap.KeyThatDoesntExist}}
    {{if eq . "mystring"}}
        Match
    {{else}}
        No match
    {{end}}
{{else}}
    Key not found
{{end}}

Full coverage with {{if}}:

{{if .MyMap.KeyThatDoesntExist}}
    {{if eq .MyMap.KeyThatDoesntExist "mystring"}}
        Match
    {{else}}
        No match
    {{end}}
{{else}}
    Key not found
{{end}}

Note that in all of the full coverage variants if key exists but associated value is "", that will also result in "Key not found".

Try these on the Go Playground.