gonfiguration
A thread-safe Go library that fills a config struct from environment variables, so you stop writing the same os.Getenv and strconv boilerplate in every service.
Tag the fields you want with env:"NAME", mark the ones that must be set with ,required, give fallbacks with a default tag or from code, and call Parse. Errors are wrapped with ctxerrors, so each one carries the file, line and function of every hop it passed through.
Install
go get github.com/psyb0t/gonfiguration
The module declares go 1.26 in its go.mod, so you need Go 1.26 or newer. Besides ctxerrors, the library imports only the standard library.
Quick look
package main
import (
"fmt"
"log"
"time"
"github.com/psyb0t/gonfiguration"
)
type Config struct {
ListenAddress string `env:"LISTEN_ADDRESS" default:"127.0.0.1:8080"`
Debug bool `env:"DEBUG" default:"false"`
Timeout time.Duration `env:"TIMEOUT" default:"30s"`
AllowedHosts []string `env:"ALLOWED_HOSTS" default:"localhost,127.0.0.1"`
DBName string `env:"DB_NAME,required"`
}
func main() {
var cfg Config
if err := gonfiguration.Parse(&cfg); err != nil {
log.Fatalf("config: %v", err)
}
fmt.Printf("%+v\n", cfg)
}
Run it with DB_NAME=myapp ALLOWED_HOSTS="api.example.com, cdn.example.com" and AllowedHosts comes out as [api.example.com cdn.example.com], the spaces trimmed. Leave DB_NAME unset and Parse returns an error that matches gonfiguration.ErrRequiredFieldNotSet.
Where to go next
| You want to | Read |
|---|---|
| Know which tags exist and how each field type is parsed | Struct tags and types |
| Set fallbacks and know which value wins | Defaults and precedence |
Match errors with errors.Is and read what they print | Errors |
| Look up a signature, or use the library from several goroutines | API reference |
| Build, lint and test the library itself | Development |
The Go package docs are on pkg.go.dev, and the source is on GitHub. A longer runnable example is in .example/main.go.