This directory contains examples demonstrating different ways to use the Conflex configuration library.
A simple example showing the most basic usage of Conflex - 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 Conflex, 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 Conflex 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.
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.goThe 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
var config MyConfig
cfg, err := conflex.New(
conflex.WithFileSource("config.yaml", codec.TypeYAML),
conflex.WithBinding(&config),
)var config MyConfig
cfg, err := conflex.New(
conflex.WithOSEnvVarSource("APP_"),
conflex.WithBinding(&config),
)var config MyConfig
cfg, err := conflex.New(
conflex.WithFileSource("config.yaml", codec.TypeYAML), // Defaults
conflex.WithOSEnvVarSource("APP_"), // Overrides
conflex.WithBinding(&config),
)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.portWEBAPP_DATABASE_PRIMARY_HOST→database.primary.host
Most examples include tests demonstrating different scenarios:
cd examples/comprehensive
go test -vWhen 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
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