我尝试过使用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)
}当我试图在构建后运行此测试代码时,它显示为空值。
&{[]}有什么问题我要解决吗?
发布于 2022-04-05 22:19:30
要从HCL中解封的结构上的字段需要导出。要导出字段,请将字段名大写中的第一个字符设置为大写。
type cfg_dict struct {
Name string `hcl:",key"`
Type string `hcl:"type"`
}
type hcl_config struct {
Config_items cfg_dict `hcl:"config"`
}https://stackoverflow.com/questions/49965175
复制相似问题