Go模板中的比较运算符在缺少映射键时的处理方式。

huangapple go评论73阅读模式
英文:

Go template comparison operators on missing map key

问题

我无法找到关于在尝试键入一个不存在键的映射时返回值类型的任何文档。从Go的错误跟踪器中可以看出,它似乎是一个特殊的“无值”。

我正在尝试使用eq函数比较两个值,但如果键不存在,则会出错。

示例:

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

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

结果是error calling eq: invalid type for comparison

从这个例子中,我可以推断出nil值不是空字符串"",就像在Go本身中一样。

有没有一种简单的方法来比较一个可能不存在的映射值和另一个值?

英文:

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?

答案1

得分: 30

使用index函数:

{{if eq (index .MyMap "KeyThatDoesntExist") "mystring"}}
  {{.}}
{{end}}

<kbd>playground example</kbd>

当键不在映射中时,index函数返回映射值类型的零值。问题中映射的零值是空字符串。

英文:

Use the index function:

{{if eq (index .MyMap &quot;KeyThatDoesntExist&quot;) &quot;mystring&quot;}}
  {{.}}
{{end}}

<kbd>playground example</kbd>

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.

答案2

得分: 3

你可以先检查键是否在映射中,只有在存在时才执行比较操作。你可以使用另一个{{if}}操作或者使用{{with}}操作来进行检查,{{with}}操作还可以设置管道。

使用{{with}}

{{with .MyMap.KeyThatDoesntExist}}{{if eq . "mystring"}}匹配{{end}}{{end}}

使用另一个{{if}}

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

请注意,你可以添加{{else}}分支来处理其他情况。使用{{with}}进行完整覆盖:

{{with .MyMap.KeyThatDoesntExist}}
    {{if eq . "mystring"}}
        匹配
    {{else}}
        不匹配
    {{end}}
{{else}}
    键未找到
{{end}}

使用{{if}}进行完整覆盖:

{{if .MyMap.KeyThatDoesntExist}}
    {{if eq .MyMap.KeyThatDoesntExist "mystring"}}
        匹配
    {{else}}
        不匹配
    {{end}}
{{else}}
    键未找到
{{end}}

请注意,在所有完整覆盖的变体中,如果键存在但关联的值为"",也会导致显示"键未找到"

你可以在Go Playground上尝试这些代码。

英文:

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 . &quot;mystring&quot;}}Match{{end}}{{end}}

Using another {{if}}:

{{if .MyMap.KeyThatDoesntExist}}
    {{if eq .MyMap.KeyThatDoesntExist &quot;mystring&quot;}}Match{{end}}{{end}}

Note that you can add {{else}} branches to cover other cases. Full coverage with {{with}}:

{{with .MyMap.KeyThatDoesntExist}}
    {{if eq . &quot;mystring&quot;}}
        Match
    {{else}}
        No match
    {{end}}
{{else}}
    Key not found
{{end}}

Full coverage with {{if}}:

{{if .MyMap.KeyThatDoesntExist}}
    {{if eq .MyMap.KeyThatDoesntExist &quot;mystring&quot;}}
        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 &quot;&quot;, that will also result in &quot;Key not found&quot;.

Try these on the Go Playground.

huangapple
  • 本文由 发表于 2016年1月21日 14:49:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/34917099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定