Golang模板并测试有效字段

时间:2018-07-04 04:11:42

标签: templates if-statement go go-templates

在Go的数据库/ sql程序包中,有一堆Null [Type]结构可帮助将数据库值(及其可能的null)映射到代码中。我试图弄清楚如何测试结构 field 是否为空,或者换句话说,当其Valid属性为false时。

建议的打印SQL字段的方法是使用.Value属性,如下所示:

<div>{{ .MyStruct.MyField.Value }}</div>

这很好用。

但是假设我有一些更复杂的东西,例如,我需要在其他地方测试该值,

<select name="y">
   {{ range .SomeSlice }}
       <option value="{{ . }}" {{ if eq $.MyStruct.MyField.Value .}}selected="selected"{{ end }}>{{ . }}</option>
   {{ end }}
</select>

碰巧的是,这也很好用,除非.MyField无效,在这种情况下,我会收到错误消息:“错误调用eq:用于比较的无效类型”。该错误是有道理的,因为Go无法将nil Field与另一个值(或类似值)进行比较。

我本以为“简单”的解决方案是先测试Value是否为nil,然后将其与所需的值进行比较,如下所示:

<select name="y">
   {{ range .SomeSlice }}
       <option value="{{ . }}" {{ if and ($.MyStruct.MyField) (eq $.MyStruct.MyField.Value .)}}selected="selected"{{ end }}>{{ . }}</option>
   {{ end }}
</select>

在这种情况下,我得到相同的“错误调用eq:用于比较的无效类型”。我猜这意味着.MyField“存在”,即使.MyField的值无效。因此,然后我尝试了六个其他版本,大多数都存在相同的错误,例如:

<select name="y">
   {{ range .SomeSlice }}
       <option value="{{ . }}" {{ if and ($.MyStruct.MyField.Valid) (eq $.MyStruct.MyField.Value .)}}selected="selected"{{ end }}>{{ . }}</option>
   {{ end }}
</select>

至此,我意识到我真的根本不了解如何测试有效字段的存在。非常感谢您的帮助。

谢谢。

1 个答案:

答案 0 :(得分:5)

Go模板中的and函数不是短路求值的(不同于Go中的&&运算符),它的所有参数总是被求值的。引用text/template软件包文档:

and
    Returns the boolean AND of its arguments by returning the
    first empty argument or the last argument, that is,
    "and x y" behaves as "if x then y else x". All the
    arguments are evaluated.

这意味着您的{{if}}动作:

{{ if and ($.MyStruct.MyField) (eq $.MyStruct.MyField.Value .)}}

即使如果false$.MyStruct.MyField的条件将被评估为nil,但是eq $.MyStruct.MyField.Value .也将被评估并导致错误。

相反,您可以嵌入多个{{if}}操作,如下所示:

{{if $.MyStruct.MyField}}
    {{if eq $.MyStruct.MyField.Value .}}selected="selected"{{end}}
{{end}}

您也可以使用{{with}}动作,但这也会设置点,因此必须小心:

<select name="y">
   {{range $idx, $e := .SomeSlice}}
       <option value="{{.}}" {{with $.MyStruct.MyField}}
               {{if eq .Value $e}}selected="selected"{{end}}
           {{end}}>{{.}}</option>
   {{end}}
</select>

注意:

您在谈论问题中的nil值,但是sql.NullXX类型是不能为nil的结构。在这种情况下,您必须检查其Valid字段以判断其Value()方法在调用时是否会返回非nil值。看起来可能像这样:

{{if $.MyStruct.MyField.Valid}}
    {{if eq $.MyStruct.MyField.Value .}}selected="selected"{{end}}
{{end}}