我有一个去项目,我想读一个HCL文件。此HCL文件包含变量。但是,我无法解析它,并得到以下错误消息:
Variables not allowed; Variables may not be used here., and 1 other diagnostic(s)我的围棋密码:
package main
import (
"fmt"
"log"
"github.com/hashicorp/hcl/v2/hclsimple"
)
type Config struct {
Hello string `hcl:"hello"`
World string `hcl:"world"`
Message string `hcl:"message"`
}
func main() {
var config Config
err := hclsimple.DecodeFile("test.hcl", nil, &config)
if err != nil {
log.Fatalf("Failed to load configuration: %s", err)
}
fmt.Println(config.Message)
}我的HCL文件
hello = "hello"
world = "world"
message = "hello ${world}"我做错了什么?我的HCL语法是否不正确?
发布于 2022-09-16 14:01:56
是不是我的HCL语法不正确?
它在语法上是有效的,但不像你期望的那样起作用。HCL不允许引用在HCL文件中其他地方定义的任意值。它只允许引用解析器公开的变量。例如,这提供了预期的输出:
ectx := &hcl.EvalContext{Variables: map[string]cty.Value{"world": cty.StringVal("world")}}
err := hclsimple.DecodeFile("test.hcl", ectx, &config)文档并没有特别清楚地说明这一点,但是相关的参考应该在这里:https://github.com/hashicorp/hcl/blob/main/guide/go_expression_eval.rst#expression-evaluation-modes
https://stackoverflow.com/questions/73744779
复制相似问题