【发布时间】:2021-11-18 19:08:03
【问题描述】:
我想使用 go 模板输出slice[0].type。打印工作时:
{{ index .Slice 0 | printf "%+v" }} // {Type:api.Meter ShortType:Meter VarName:meter}
打印字段不会:
{{ index .Slice 0 | .Type }} // template: gen:43:26: executing "gen" at <.Type>: can't evaluate field Type in type struct { API string; Package string; Function string; BaseTypes []main.baseType; Types map[string]main.typeStruct; Combinations [][]string }
错误消息表明外部循环字段被评估,而不是index调用的结果。
访问切片嵌套字段的正确语法是什么?
【问题讨论】:
标签: go go-templates