我尝试做这个测试来弄清楚如何设置这样的条件:
<h1>Country Index</h1>
<style>
.odd{
background: orange;
color: black;
font-size: 1.5em;
}
.even{
background: rgb(0, 121, 235);
color: white;
font-size: 1.5em;
}
</style>
<ul>
{{ range $index, $item := .Tee }}
{{ if $index % 2 == 0 }}
<li class="even">{{ $index }} - {{ $item }}</li>
{{ else }}
<li class="odd">{{ $index }} - {{ $item }}</li>
{{ end }}
{{ end }}
</ul>
我收到此错误“操作数中出现意外的“%””。
有什么建议可以解决这个问题吗?
最佳答案
遗憾的是,您不能在模板中使用 +、-、*、/或 % 等运算符。相反,您必须编写自定义函数并使用 funcMap 将它们带入您的模板中。
下面是 Go Playground 上的一个示例,它可以检测应用于稍微修改的模板文本版本的偶数整数。
关于go - 如何在golang模板中的LOOP内执行IF/ELSE条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51048852/