使用Go模板中的变量键访问地图值 [英] Access a map value using a variable key in a Go template
本文介绍了使用Go模板中的变量键访问地图值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过使用变量键而不迭代来查找映射的值?
所以你可以在变量映射$ x上用 $ x.key1
查找一个常量键,但有可能要做 amap。$ key
?
解决方案
c $ c> index 函数:
{{index .Amapkey1}}
从 http ://golang.org/pkg/text/template/ :
索引
返回结果通过
后面的参数来索引它的第一个参数。因此,在Go语法中,index x 1 2 3是
x [1] [2] [3]。每个索引项目必须是地图,切片或数组。
How can I look up the value of a map by using a variable key without iterating?
So one can lookup a constant key on variable map $x with $x.key1
, but is it possible to do amap.$key
?
解决方案
You use the index
function:
{{index .Amap "key1"}}
From http://golang.org/pkg/text/template/:
index
Returns the result of indexing its first argument by the
following arguments. Thus "index x 1 2 3" is, in Go syntax,
x[1][2][3]. Each indexed item must be a map, slice, or array.
这篇关于使用Go模板中的变量键访问地图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文