小编典典

在 Go 模板中使用可变键访问映射值

go

如何在不迭代的情况下使用变量键查找地图的值?

因此,可以使用 查找变量映射 $x 上的常量键$x.key1,但是可以这样做amap.$key吗?


阅读 206

收藏
2021-12-13

共1个答案

小编典典

您使用该index功能:

{{index .Amap "key1"}}
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.

https://golang.org/pkg/text/template#hdr-Functions

2021-12-13