config
Config Package
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.
Features
Environment-First Configuration: Automatically reads from environment variables.
YAML File Support: Load config from
.yamlfiles (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.Contextor*http.Request.
Installation
go get github.com/kittipat1413/go-common/framework/configDocumentation
For detailed API documentation, examples, and usage patterns, visit the Go Package Documentation.
Usage
This is the simplest way to get started using MustConfig with some defaults:
With an optional env.yaml override:
Examples
You can find a complete working example in the repository under framework/config/example.
Functional Options
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.
Accessing Values
Last updated