如何知道范围是否为空?

How to know if a range is empty?

如果我的范围为空,我如何有条件地显示其他内容?

{{range .Users}}
...
{{end}}

如果范围为空,我想显示HTML的不同块。

使用{{range pipeline}} T1 {{else}} T0 {{end}}:

{{range pipeline}} T1 {{else}} T0 {{end}}
        The value of the pipeline must be an array, slice, map, or channel.
        If the value of the pipeline has length zero, dot is unaffected and
        T0 is executed; otherwise, dot is set to the successive elements
        of the array, slice, or map and T1 is executed.

示例:

{{range .Users}}
...
{{else}}
<p>No users</p>
{{end}}