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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -