Config Examples
This directory contains examples demonstrating different ways to use the Config package.
Examples Overview
A simple example showing the most basic usage of Config package - loading configuration from a YAML file into a Go struct.
Features:
- File source (YAML)
- Struct binding
- Type conversion
- Nested structures
- Arrays and slices
- Time and URL types
Best for: Getting started with Config package, understanding basic concepts.
Demonstrates loading configuration from environment variables, following the Twelve-Factor App methodology.
Features:
- Environment variable source
- Struct binding
- Nested configuration
- Direct access methods
- Type conversion
Best for: Containerized applications, cloud deployments, following 12-factor app principles.
Shows how to combine YAML files and environment variables, with environment variables overriding YAML defaults.
Features:
- Mixed configuration sources
- Configuration precedence
- Environment variable mapping
- Struct binding
- Direct access
Best for: Applications that need both default configuration files and environment-specific overrides.
A complete example demonstrating advanced Config package features with a realistic web application configuration.
Features:
- Mixed configuration sources
- Complex nested structures
- Validation
- Comprehensive testing
- Production-ready patterns
- Docker examples
Best for: Production applications, learning advanced features, understanding best practices.
Quick Start
Choose the example that best fits your needs:
# Basic YAML configuration
cd examples/basic
go run main.go
# Environment variables only
cd examples/environment
export WEBAPP_SERVER_HOST=localhost
export WEBAPP_SERVER_PORT=8080
go run main.go
# Mixed YAML + environment variables
cd examples/mixed
export WEBAPP_SERVER_PORT=8080 # Override YAML default
go run main.go
# Comprehensive example with tests
cd examples/comprehensive
go test -v
go run main.go
Example Progression
The examples are designed to be progressive:
- Basic: Start here to understand core concepts
- Environment: Learn about environment variable configuration
- Mixed: Understand configuration precedence and mixed sources
- Comprehensive: See advanced features and production patterns
Common Patterns
Basic YAML Configuration
var config MyConfig
cfg, err := config.New(
config.WithFileSource("config.yaml", codec.TypeYAML),
config.WithBinding(&config),
)
Environment Variables Only
var config MyConfig
cfg, err := config.New(
config.WithOSEnvVarSource("APP_"),
config.WithBinding(&config),
)
Mixed Configuration (Recommended for Production)
var config MyConfig
cfg, err := config.New(
config.WithFileSource("config.yaml", codec.TypeYAML), // Defaults
config.WithOSEnvVarSource("APP_"), // Overrides
config.WithBinding(&config),
)
Environment Variable Naming
All examples use a consistent environment variable naming convention:
- Prefix: Configurable (e.g.,
WEBAPP_, APP_)
- Hierarchy: Underscores create nested levels
- Conversion: Keys are converted to lowercase
Examples:
WEBAPP_SERVER_PORT → server.port
WEBAPP_DATABASE_PRIMARY_HOST → database.primary.host
Testing
Most examples include tests demonstrating different scenarios:
cd examples/comprehensive
go test -v
Production Considerations
When using these examples in production:
- Use mixed configuration for flexibility
- Implement validation for required fields
- Use secrets management for sensitive values
- Follow 12-factor app principles for configuration
- Add comprehensive logging for debugging
Contributing
When adding new examples:
- Create a new directory with a descriptive name
- Include a comprehensive README.md
- Add tests if applicable
- Update this main README.md
- Follow the existing patterns and conventions