From 46f91bd1399e16fa706d43c86914d95824c7fdfb Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 1 Oct 2019 16:20:52 -0700 Subject: [PATCH] README: include a short Go example using the hclsimple API --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 61f26a8..7b7ac71 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,31 @@ names and nested block types are expected, and HCL parses the configuration file, verifies that it conforms to the expected structure, and returns high-level objects that the application can use for further processing. +```go +package main + +import ( + "log" + "github.com/hashicorp/hcl/v2/hclsimple" +) + +type Config struct { + LogLevel string `hcl:"log_level"` +} + +func main() { + var config Config + err := hclsimple.DecodeFile("config.hcl", nil, &config) + if err != nil { + log.Fatalf("Failed to load configuration: %s", err) + } + log.Printf("Configuration is %#v", config) +} +``` + +A lower-level API is available for applications that need more control over +the parsing, decoding, and evaluation of configuration. + ## Why? Newcomers to HCL often ask: why not JSON, YAML, etc?