Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go template comparison operators on missing map key

I am unable to find any documentation regarding what the type of the return value is when attempting key into a map in which the key doesn't exist. From the Go bug tracker it appears to be a special 'no value'

I'm trying to compare two values using the eq function but it gives an error if the key doesn't exist

Example:

var themap := map[string]string{}  
var MyStruct := struct{MyMap map[string]string}{themap}

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

Results in error calling eq: invalid type for comparison

From this I assume that the nil value is not the empty string "" as it is in Go itself.

Is there a simple way to compare a potentially non-existent map value and another value?

like image 389
Sam Avatar asked Jan 21 '16 06:01

Sam


People also ask

Can slice Be map key in Golang?

No, slices cannot be used as map keys as they have no equality defined. Save this answer.

What is Gomplate?

gomplate is a template renderer which supports a growing list of datasources, such as: JSON (including EJSON - encrypted JSON), YAML, AWS EC2 metadata, and secrets. Come chat with developers and community in the on and on !


2 Answers

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.

like image 146
Bayta Darell Avatar answered Oct 24 '22 06:10

Bayta Darell


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.

like image 3
icza Avatar answered Oct 24 '22 06:10

icza