Go template comparison operators on missing map key -


i unable find documentation regarding type of return value when attempting key map in key doesn't exist. go bug tracker appears special 'no value'

i'm trying compare 2 values using eq function gives error if 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 comparison

from assume nil value not empty string "" in go itself.

is there simple way compare potentially non-existent map value , value?

use index function:

{{if eq (index .mymap "keythatdoesntexist") "mystring"}}   {{.}} {{end} 

playground example

the index function returns 0 value map value type when key not in map. 0 value map in question empty string.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -