问题描述

我正在尝试比较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
.SearchData
{{if not .SearchData}} Nothing to show {{end}}
"0"0
"0"0
{{ $length := len .SearchData }} {{ if eq $length 0 }}
    Sorry. No matching results found
{{ end }}

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