【问题标题】:Can't evaluate field <field> in type string无法评估字符串类型中的字段 <field>
【发布时间】:2019-04-01 03:48:03
【问题描述】:

我在 golang 包中使用 text/template 来解析带有环境的 json 字符串中的文本。

示例:https://play.golang.org/p/uARnrOyG4Th

但我有一个错误:2009/11/10 23:00:00 Execute: template: Person template:1:19: executing "Person template" at &lt;.id&gt;: can't evaluate field id in type string

请帮我解决问题。根据图书馆的文件,我找不到错误。

【问题讨论】:

  • person 是代码中的字符串,而不是结构。如果您将 JSON 解组为结构值,则模板代码可以使用反射来访问 id 等字段。

标签: go go-templates


【解决方案1】:

模板不能直接应用于字符串,你应该创建一个结构然后将你的 JSON 字符串解组到结构实例。 try the code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2022-08-16
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多