如何在golang中的html/template中比较列表的长度? [英] How to compare the length of a list in html/template in golang?

查看:1090
本文介绍了如何在golang中的html/template中比较列表的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较golang html/template中列表的长度.但是它会永远以html的形式加载.

I am trying to compare the length of a list in golang html/template. But it is loading forever in html.

{{ $length := len .SearchData }} {{ if eq $length "0" }}
    Sorry. No matching results found
{{ end }}

有人可以帮我吗?

推荐答案

从文档开始,

{{if管道}} T1 {{end}}:如果管道的值为空,则否 产生输出;否则,执行T1.空值为 false,0,任何nil指针或接口值以及任何数组,切片, 映射或长度为零的字符串.点不受影响.

{{if pipeline}} T1 {{end}}: If the value of the pipeline is empty, no output is generated; otherwise, T1 is executed. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero. Dot is unaffected.

因此,如果您想检查.SearchData slice/array/map是否为空,请使用

So if you want to check if the .SearchData slice/array/map is empty just use,

{{if not .SearchData}} Nothing to show {{end}}

如果将字符串"0"替换为int 0

Even your code runs fine if string "0" is replaced by int 0

{{ $length := len .SearchData }} {{ if eq $length 0 }}
    Sorry. No matching results found
{{ end }}

http://play.golang.org/p/Q44qyRbKRB

这篇关于如何在golang中的html/template中比较列表的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆