The config package is responsible for managing application configuration. It provides a centralized approach to loading, validating, and accessing configuration values from embedded files, environment variables, and defaults.
Go Validator to ensure all required fields are populated and valid.
An embedded default configuration file (default.ini) as a baseline, which can be overridden by environment variables.
Features
Centralized Configuration:
Loads default values from an embedded default.ini file.
Overrides defaults with values from environment variables.
Validation:
Ensures critical configuration fields are present and valid using validator.
Flexibility:
Uses the ini format for defaults but allows customization via environment variables.
Environment variables are automatically mapped by replacing . with _.
Environment Variable Overrides
Environment variables can override default.ini values.
Format: Replace . with _ in variable names.
Example:
app.name → APP_NAME
database.password → DATABASE_PASSWORD
Best Practices
Keep Secrets Secure:
Avoid committing sensitive values (e.g., passwords, secrets) to version control. Use environment variables for sensitive data.
Validate Configuration Early:
Ensure that configuration validation is performed during application startup to catch issues early.
Use Environment Variables for Deployment:
Rely on environment variables to override default values in different environments (e.g., production, staging).