【发布时间】:2020-03-02 20:05:56
【问题描述】:
我在 golang 模板代码 sn-p 下面有这个,我从 map[string]interface{} 类型的映射中获取值并检查该字符串是否为空,但我的字符串空检查失败为:
template: apps.html:62:29: executing "apps.html" at <eq $src "">: error calling eq: invalid type for comparison 。我也尝试打印空值,它呈现为<nil>,但我的{{if eq $src "<nil>"}} 检查也失败了,即使我输入nil,它也会失败。有没有更好的方法来实现这一点。
{{$src := (index . "source")}}
{{$tar := (index . "target")}}
{{if eq $src ""}}
<div></div>
{{else}}
<div style="display:none;">
<input id="username" name="source" value="{{ $src }}"/>
<input id="username" name="target" value="{{ $tar }}"/>
</div>
{{end}}
【问题讨论】:
标签: string go go-templates