config
Last updated
Last updated
The config package provides a reusable and extensible configuration loader built on top of Viper
. It is designed to simplify configuration management in Go applications by supporting multiple config sources, context propagation, and functional loading options.
Environment-First Configuration: Automatically reads from environment variables.
YAML File Support: Load config from .yaml
files (required or optional).
Injectable Defaults: Provide fallback values when env or file values are not present.
Context Integration: Easily inject and retrieve config via context.Context
or *http.Request
.
This is the simplest way to get started using MustConfig
with some defaults:
With an optional env.yaml
override:
WithRequiredConfigPath(path string)
: Fails if the file does not exist or is unreadable.
WithOptionalConfigPaths(path string)
: Tries each path in order and uses the first found file. Skips missing files.
WithDefaults(defaults map[string]any)
: Injects fallback values if the config key is not set in env or file.
You can find a complete working example in the repository under .