如何在Golang模板中检查字符串的空值。

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

how to check empty value of a string in golang template

问题

我有以下的 Golang 模板代码片段,其中我从一个类型为 map[string]interface{} 的映射中获取值,并检查该字符串是否为空。但是我的字符串为空的检查失败了,错误信息为:template: apps.html:62:29: executing "apps.html" at <eq $src "">: error calling eq: invalid type for comparison。我尝试打印空值,结果显示为 &lt;nil&gt;,但是我的 {{if eq $src "&lt;nil&gt;"}} 检查也失败了,即使我将其设置为 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}}
英文:

I have this below golang template code snippet where I take values from a map of type map[string]interface{} and check if that string is empty or not but my string empty check is failing as:
template: apps.html:62:29: executing &quot;apps.html&quot; at &lt;eq $src &quot;&quot;&gt;: error calling eq: invalid type for comparison . I tried to print the empty value also and it is rendered as &lt;nil&gt; but my {{if eq $src &quot;&lt;nil&gt;&quot;}} check is also failing and even if I put nil then also it fails. Is there any better way to achieve this.

    {{$src := (index . &quot;source&quot;)}}
    {{$tar := (index . &quot;target&quot;)}}
    {{if eq $src &quot;&quot;}}
          &lt;div&gt;&lt;/div&gt;
    {{else}}
          &lt;div style=&quot;display:none;&quot;&gt;
              &lt;input id=&quot;username&quot; name=&quot;source&quot; value=&quot;{{ $src }}&quot;/&gt;
              &lt;input id=&quot;username&quot; name=&quot;target&quot; value=&quot;{{ $tar }}&quot;/&gt;
          &lt;/div&gt;
    {{end}}

答案1

得分: 18

这是你正在做的事情(如果可能的话,最好给出一个示例play.golang.org链接):

https://play.golang.org/p/uisbAr_3Qy

你所做的一些问题:如果你在使用一个map作为上下文,你根本不需要使用索引,所以你的变量是不需要的。如果你想检查一个键是否存在,只需使用if检查一个nil条目。如果你的map包含类型为interface的元素,你不能与字符串进行比较,只有在你确定有一个元素但不确定它可能是什么时,才使用eq,并在if测试中包装它,以确定键是否存在。

我认为你想做的是这样的:

{{if .source }}
  <div style="display:none;">
	<input id="username" name="source" value="{{ .source }}"/>
	<input id="username" name="target" value="{{ .target }}"/>
  </div>
{{else}}
  <div>empty</div>
{{end}}

https://play.golang.org/p/D2DCjAklFE

请参阅text/template的文档,它们有更多详细信息:

https://golang.org/pkg/text/template/#hdr-Actions

英文:

Here is what you're doing (always better to give an example play.golang.org link if possible):

https://play.golang.org/p/uisbAr_3Qy

A few problems with what you're doing: If you're using a map for context, you don't need to use index at all, so your variables are not required. If you want to check if a key exists, just check for a nil entry with if. If your map contains elements of type interface, you can't compare with a string, only use eq when you're sure you have an element but are not sure what it might be, and wrap it in an if test if unsure whether the key exists.

I think you want to do something like this:

{{if .source }}
  &lt;div style=&quot;display:none;&quot;&gt;
	&lt;input id=&quot;username&quot; name=&quot;source&quot; value=&quot;{{ .source }}&quot;/&gt;
	&lt;input id=&quot;username&quot; name=&quot;target&quot; value=&quot;{{ .target }}&quot;/&gt;
  &lt;/div&gt;
{{else}}
  &lt;div&gt;empty&lt;/div&gt;
{{end}}

https://play.golang.org/p/D2DCjAklFE

See the docs for text/template as they have a lot more detail:

https://golang.org/pkg/text/template/#hdr-Actions

答案2

得分: 0

如果你想与null(nil)进行比较,请使用以下链接:https://github.com/Masterminds/sprig/issues/53#issuecomment-483414063

{{ kindIs "invalid" $value }}
英文:

If you want to compare with null(nil) use: https://github.com/Masterminds/sprig/issues/53#issuecomment-483414063

{{ kindIs &quot;invalid&quot; $value }}

huangapple
  • 本文由 发表于 2017年2月24日 17:04:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/42434592.html
匿名

发表评论

匿名网友

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

确定