Reliable Application Configuration from Environment Variables in Go
Environment variables are a convenient way to configure deployed Go services, but calling os.Getenv throughout an application makes configuration difficult to validate and test.
A stronger pattern is to load configuration once at startup, parse it into typed fields, validate all invariants, and pass the resulting value to the components that need it.
The examples below use only the Go standard library and work with modern supported Go releases.
Keep configuration in a typed struct Suppose a service needs a listen address, request timeout, and optional log level: