英文:
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
。我尝试打印空值,结果显示为 <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}}
英文:
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 "apps.html" at <eq $src "">: error calling eq: invalid type for comparison
. I tried to print the empty value also and it is rendered as <nil>
but my {{if eq $src "<nil>"}}
check is also failing and even if I put nil
then also it fails. Is there any better way to achieve this.
{{$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}}
答案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 }}
<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
See the docs for text/template as they have a lot more detail:
答案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 "invalid" $value }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论