如何知道 golang 模板中传递的值是什么?

问题描述

有没有办法通过只修改 golang 模板(而不是渲染器)来获取 golang 模板中打印的每个值的每个结构成员(就像 spewSprintf("%#v")json.Marshal) ,因为渲染器是已经编译和运行的 3rd 方程序)?

情况是,我正在尝试正确打印 prometheusalertmanager,但我得到的只是

Alerts Firing:
- :
- :
- :
- :

使用这个模板:

*Alerts Firing:*
{{ range .Alerts.Firing }}- {{ .Annotations.identifier }}: {{ .Annotations.description }}
{{ end }}

有没有办法从 .获取所有可能的值?

解决方法

我的意思是仅从外观来看,我会认为问题在于 .identifier.description 要么未导出,要么只需要以大写字母开头:

{{ range .Alerts.Firing }}- {{ .Annotations.identifier }}: {{ .Annotations.description }}

可能只是需要

{{ range .Alerts.Firing }}- {{ .Annotations.Identifier }}: {{ .Annotations.Description }}

或者它是一张地图,你只需要:

{{ range .Alerts.Firing }}- {{ index .Annotations "identifier" }}: {{ index .Annotations "description" }}