我将使用this repository解析HCL配置文件。
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
?
答案 0 :(得分:4)
github.com/hashicorp/hcl/hcl/parser是一个低级别的软件包。改为使用high-level API:
.*
如果您真的想自己处理AST,还可以使用DecodeObject。