Simple, flexible, and secure configuration loader for Go.
Load configurations from files, environment variables, and safely bind them into structs — with built-in secrets masking.
Inspired by Rust crate config.
- Load from YAML files and environment variables
- Load from .env files
- Merge multiple sources with priority
- Bind into strongly-typed structs using tags
- Minimalistic, clean API
- Mark required fields with
required:"true" - Mask sensitive fields for secure logging
go get github.com/shkmv/goconfigpackage main
import (
"fmt"
"github.com/shkmv/goconfig"
)
type ServerConfig struct {
Port int `config:"port"`
DBHost string `config:"db.host"`
DBPass string `config:"db.pass"`
}
func main() {
var cfg ServerConfig
err := goconfig.New().
FromFile("server-config.yaml").
FromDotEnv(".env").
FromEnv("APP_").
Bind(&cfg)
if err != nil {
panic(err)
}
}
_ = cfgAdd required:"true" to any field with a config tag to fail binding when the key is missing in all sources.
type ServerConfig struct {
Port int `config:"port" required:"true"`
DBHost string `config:"db.host" required:"true"`
DBPass string `config:"db.pass"` // optional
}Mark sensitive fields with secret:"true" and use MaskedJSON (or MaskedMap) when logging configuration.
type ServerConfig struct {
Port int `config:"port"`
DBHost string `config:"db.host"`
DBPass string `config:"db.pass" secret:"true"`
}
cfg, _ := goconfig.Load[ServerConfig](
goconfig.WithFile("server-config.yaml"),
)
safe, _ := goconfig.MaskedJSON(cfg)
fmt.Printf("config: %s\n", safe) // db.pass is "***"package main
import (
"fmt"
"github.com/shkmv/goconfig"
)
type ServerConfig struct {
Port int `config:"port"`
DBHost string `config:"db.host"`
DBPass string `config:"db.pass"`
}
func main() {
cfg, err := goconfig.Load[ServerConfig](
goconfig.WithFile("server-config.yaml"),
goconfig.WithDotEnv(".env"),
goconfig.WithEnv("APP_"),
)
if err != nil {
panic(err)
}
_ = cfgSimple KEY=VALUE lines are supported. Lines beginning with # are comments. Optional export is allowed. Inline comments after unescaped # are stripped. Quotes and a few escapes (\n, \t, \r, \) are handled.
Keys are normalized like environment variables: underscores become dots and keys are lowercased. For example DB_HOST=localhost becomes db.host.