如何通过golang解析每个HCL字典项?

时间:2018-04-22 11:00:32

标签: go hcl

我尝试使用golang解析HCL配置,但它无效。

type cfg_dict struct {
      name     string       `hcl:",key"`
      type     string       `hcl:"type"`
}

type hcl_config struct {
      config_items    cfg_dict      `hcl:"config"`
}

func main() {
    hcl_example = `config "cfg1" {
           type = "string"
    }`

    hcl_opts := &hcl_config{}

    hcl_tree, err := hcl.Parse(hcl_example)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    if err := hcl.DecodeObject(&hcl_opts, hcl_tree); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Println(hcl_opts)
}

当我在构建之后尝试运行此测试代码时,它显示空值。

&{[]}

我有什么问题需要解决吗?

0 个答案:

没有答案