迭代Go地图获取索引 [英] Iterate Go map get index
问题描述
为了使用revel
的 even
关键字在模板中,我想使用range
进行迭代时获取映射条目的索引.有什么办法吗?
我的地图具有以下结构:
In order to use revel
's even
keyword in templates I would like to get the index of a map entry when iterating with range
. Is there any way to do so?
My map has the structure:
map[string][]string
推荐答案
您不能仅使用模板操作来执行此操作,但是您可以注册一个提供必要帮助的函数.
You can't do this only with template actions, but you may register a function which provides the necessary help.
您可以注册一个返回一个函数(闭包)的函数,该函数在每次调用时都会改变其返回值(确切地说,奇数"和偶数"索引是如何交替的):
You may register a function which returns a function (closure), which alternates its return value whenever called (exactly how "odd" and "even" indices alternate):
func isEven() func() bool {
e := false
return func() bool {
e = !e
return e
}
}
我将其命名为isEven()
,以免与ravel的even()
发生冲突.使用它:
I named it isEven()
to not collide with ravel's even()
. Using it:
func main() {
t := template.Must(template.New("").Funcs(template.FuncMap{
"isEven": isEven,
}).Parse(templ))
m := map[string]string{
"a": "A", "b": "B", "c": "C", "d": "D",
}
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
}
const templ = `{{$e := isEven}}
{{- range $k, $v := . -}}
[even:{{call $e}}] key={{$k}}; value={{$v}}
{{end}}`
输出(在游乐场上尝试):
[even:true] key=a; value=A
[even:false] key=b; value=B
[even:true] key=c; value=C
[even:false] key=d; value=D
如果您想为奇数和偶数迭代提供不同的输出,则可以在{{if}}
动作中调用$e
,如下所示:
If you want different output for odd and even iterations, you can call $e
in an {{if}}
action, like this:
const templ = `{{$e := isEven}}
{{- range $k, $v := . -}}
[{{if call $e}}even{{else}}odd {{end}}] key={{$k}}; value={{$v}}
{{end}}`
此输出(在游乐场上尝试):
[even] key=a; value=A
[odd ] key=b; value=B
[even] key=c; value=C
[odd ] key=d; value=D
引擎盖下
此模板操作:
Under the hood
This template action:
{{$e := isEven}}
创建一个名为$e
的新模板变量,其值将为isEven()
函数调用的结果(返回值). isEven()
返回一个函数值,该闭包可以访问类型为bool
的局部变量e
.稍后执行{{call $e}}
时,您没有在调用isEven()
Go函数,而是返回了该函数(闭包)并存储在$e
中.该闭包引用了本地bool
变量e
,直到可以访问isEvent()
返回的函数之前,它不会释放".
Creates a new template variable named $e
, and its value will be the result (return value) of the isEven()
function call. isEven()
returns a function value, a closure that has access to a local variable e
of type bool
. When later you do {{call $e}}
, you're not calling the isEven()
Go function, but the function it returned (the closure) and is stored in $e
. That closure has a reference to the local bool
variable e
, it is not "freed" until the function returned by isEvent()
is accessible.
因此,每当执行{{call $e}}
时,它都会调用闭包,该闭包具有"类型为bool
的e
变量,该变量的值在两次$e
的调用之间保留.
So whenever you do {{call $e}}
, it calls the closure, which "has" an e
variable of type bool
, whose value is retained between calls of this $e
.
如果您再次在模板中调用isEvent
,则将返回一个新函数(闭包),包装一个局部变量e
的新实例,而与由该包装返回的闭包的第一个包装变量无关.第一次isEvent()
呼叫.
If you would call isEvent
in the template again, that would return a new function (closure), wrapping a new instance of the local variable e
, being independent of the first wrapped variable of the closure returned by the first isEvent()
call.
这篇关于迭代Go地图获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!