回答

收藏

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

技术问答 技术问答 288 人阅读 | 0 人回复 | 2023-09-12

变量键如何在不迭代的情况下找到地图值?, q. S: ?5 v# ]8 g
因此,可以使用 搜索变量映射 $x 上常量键$x.key但是可以这样做amap.$key吗?9 l0 a) d5 N7 ^2 `$ q
                                                                $ o  d: z! `& [- W* Y
    解决方案:                                                                ' o. b& k! L& v" I+ c- M. l, c% u9 S
                                                                您使用该index功能:: Z! z) B" g; |. ]) c6 [5 g
    {{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.
    " w  M5 S4 Q5 W6 D5 i4 E
https://golang.org/pkg/text/template#hdr-Functions
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则