config/app.yaml
env: dev
port: 8080
host: localhost
listen: !tpl |
{{ config "app.host" }}:{{ config "app.port" }}
db_host: !ref storage.db.host
database: !tpl |
mysql://{{ config "storage.db.user" }}:{{ config "storage.db.password" }}@{{ config "storage.db.host" }}:{{ config "storage.db.port" }}
external: !include
file:
path: external/test.yaml
parse: true
key: valueconfig/storage/db.yaml
host: localhost
port: 3306
user: rootconfig/prod/app.yaml
env: prod
port: 80config/prod/storage/db.yaml
host: remote-address
password: supersecretconfig/external/test.yaml
value: hello worldmain.go
var defaultYaml []byte // config/app.yaml
var envYaml []byte // config/prod/app.yaml
var defaultDbYaml []byte // config/storage/db.yaml
var envDbYaml []byte // config/prod/storage/db.yaml
loader := gofigure.New().WithFeatures(
feature.Reference(),
feature.Template()/*.WithFuncs(template.Funcs{}).WithValeus(map[stirng]any{}) */,
feature.Include(os.DirFS("./config")),
)
_ = loader.Load("app.yaml", defaultYaml)
_ = loader.Load("storage/db.yaml", defaultDbYaml)
_ = loader.Load("app.yaml", envYaml)
_ = loader.Load("storage/db.yaml", envDbYaml)
var app struct {
Env string `yaml:"env"`
Listen string `yaml:"listen"`
Database string `yaml:"database"`
External string `yaml:"external"`
}
_ = loader.Get(context.Background(), "app", &app)
fmt.Println(app.Env) // prod
fmt.Println(app.Listen) // localhost:80
fmt.Println(app.Database) // mysql://root:supersecret@remote-address:3306
fmt.Println(app.External) // hello world GoFigure is a tool to allow maximum flexibility in configuration loading and parsing. It is designed to be simple to use, yet powerful and extensible. It comes with default features like include other files, render a go template and reference other values, etc.
You can easily extend GoFigure with your own features with ease, please check feature for examples.