Dictionary 缺少映射键时的Go模板比较运算符
我找不到任何文档,说明在尝试向不存在键的映射中输入键时返回值的类型。从Go bug追踪器上看,它似乎是一个特殊的“无价值” 我试图使用Dictionary 缺少映射键时的Go模板比较运算符,dictionary,go,go-templates,Dictionary,Go,Go Templates,我找不到任何文档,说明在尝试向不存在键的映射中输入键时返回值的类型。从Go bug追踪器上看,它似乎是一个特殊的“无价值” 我试图使用eq函数比较两个值,但如果该键不存在,它会给出一个错误 例如: var themap := map[string]string{} var MyStruct := struct{MyMap map[string]string}{themap} {{if eq .MyMap.KeyThatDoesntExist "mystring"}} {{.}} {{e
eq
函数比较两个值,但如果该键不存在,它会给出一个错误
例如:
var themap := map[string]string{}
var MyStruct := struct{MyMap map[string]string}{themap}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}
{{.}}
{{end}
调用eq时出现错误:用于比较的类型无效
由此,我假设nil值不是Go本身的空字符串“
”
是否有一种简单的方法来比较一个可能不存在的映射值和另一个值?您可以首先检查键是否在映射中,并且仅在键在映射中时执行比较。您可以使用另一个
{{if}}
操作或同样设置管道的{{{with}}
操作进行检查
使用{{with}}
:
{{with .MyMap.KeyThatDoesntExist}}{{if eq . "mystring"}}Match{{end}}{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}Match{{end}}{{end}}
{{with .MyMap.KeyThatDoesntExist}}
{{if eq . "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
使用另一个{{if}
:
{{with .MyMap.KeyThatDoesntExist}}{{if eq . "mystring"}}Match{{end}}{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}Match{{end}}{{end}}
{{with .MyMap.KeyThatDoesntExist}}
{{if eq . "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
注意,您可以添加{{else}
分支来覆盖其他情况。完全覆盖{{with}}
:
{{with .MyMap.KeyThatDoesntExist}}{{if eq . "mystring"}}Match{{end}}{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}Match{{end}}{{end}}
{{with .MyMap.KeyThatDoesntExist}}
{{if eq . "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
完全覆盖{{if}
:
{{with .MyMap.KeyThatDoesntExist}}{{if eq . "mystring"}}Match{{end}}{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}Match{{end}}{{end}}
{{with .MyMap.KeyThatDoesntExist}}
{{if eq . "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
{{if .MyMap.KeyThatDoesntExist}}
{{if eq .MyMap.KeyThatDoesntExist "mystring"}}
Match
{{else}}
No match
{{end}}
{{else}}
Key not found
{{end}}
请注意,在所有全覆盖变体中,如果密钥存在,但关联的值为”
,则也会导致“找不到密钥”
在上尝试这些功能。使用索引功能:
{{if eq (index .MyMap "KeyThatDoesntExist") "mystring"}}
{{.}}
{{end}}
当键不在映射中时,
index
函数返回映射值类型的零值。问题中map的零值是空字符串。也适用于较旧版本的Go,与使用{{if}/{{with}}@MuffinTop的解决方案不同,我可以理解Qs&As的适度性,但注释范围可能更广,难道他们不能吗?如果没有文本列表的键可能作为布尔值存在,并且可能是false
,那么这项工作是否成功?似乎如果index.MyMap“booleanValue”
为false,则if index.MyMap“booleanValue”将失败。你知道一种检查布尔值是否存在的方法吗?答案很好,我只想指出我发现的一个警告。如果键的值为空字符串,则此操作无效@LiamKelly是的,您是对的,因为值类型的零值被视为false
条件。另一个(被接受的)答案也是如此。