本文介绍了任何人都可以帮助解析HCL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将使用此存储库来解析HCL配置文件.
I'm going to parse HCL configuration file using this repository.
package main
import (
"fmt"
hclParser "github.com/hashicorp/hcl/hcl/parser"
)
const (
EXAMPLE_CONFIG_STRING = "log_dir = \"/var/log\""
)
func main() {
// parse HCL configuration
if astFile, err := hclParser.Parse([]byte(EXAMPLE_CONFIG_STRING)); err == nil {
fmt.Println(astFile)
} else {
fmt.Println("Parsing failed.")
}
}
在这种情况下如何解析 log_dir
?
How can I parse log_dir
in this case?
推荐答案
github.com/hashicorp/hcl/hcl/parser是一个低级软件包.使用高级API 代替:
github.com/hashicorp/hcl/hcl/parser is a low-level package. Use the high-level API instead:
package main
import (
"fmt"
"github.com/hashicorp/hcl"
)
type T struct {
LogDir string `hcl:"log_dir"`
}
func main() {
var t T
err := hcl.Decode(&t, `log_dir = "/var/log"`)
fmt.Println(t.LogDir, err)
}
如果您真的想自己处理AST,也可以使用DecodeObject.
There is also DecodeObject available if you really want to deal with the AST yourself.
这篇关于任何人都可以帮助解析HCL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!