You can first check if the key is on the map, and only perform a comparison, if any. You can check with another action {{if}} or with the action {{with}} , which also sets up 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 .MyMap.KeyThatDoesntExist}} {{if eq . "mystring"}} Match {{else}} No match {{end}} {{else}} Key not found {{end}}
Full coverage {{if}} :
{{if .MyMap.KeyThatDoesntExist}} {{if eq .MyMap.KeyThatDoesntExist "mystring"}} Match {{else}} No match {{end}} {{else}} Key not found {{end}}
Note that in all full coverage options, if the key exists, but the associated value is "" , this will also lead to "Key not found" .
Try them on the go playground .
source share