我正在执行一个包含两个并行数组(大小相同)的模板,我想并行列出两个数组中的项,如何在范围内使用索引?
这显然行不通:
{{range $i, $e := .First}}$e - {{index .Second $i}}{{end}}
我正在执行一个包含两个并行数组(大小相同)的模板,我想并行列出两个数组中的项,如何在范围内使用索引?
这显然行不通:
{{range $i, $e := .First}}$e - {{index .Second $i}}{{end}}
预定义的global template functions中的一个是index
.
index
返回对其第一个参数编制索引的结果 以下是论据.因此,在围棋语法中,index x 1 2 3
是x[1][2][3]
.每个索引项必须是map
、slice
或array
.
所以你在正确的轨道上.唯一的问题是,你没有考虑到dot
已经在range
区块内重新分配.
所以你需要回到原来的点,为此我们有以下内容
执行开始时,将
$
设置为传递给EXECUTE的数据参数,即设置为点的起始值.
因此(假设您的模板中没有发生任何其他事情),您应该能够执行以下操作:
{{range $i, $e := .First}}$e - {{index $.Second $i}}{{end}}
Personally though, I would create a template function called 100 that accepts multiple slices and returns a slice of each pair of values. It would look cleaner in your template and probably get reused somewhere.