Design a robust configuration system for a Go service with env vars, validation, defaults, and environment overrides.
## CONTEXT My Go service needs a clean configuration system that loads from environment variables, applies sensible defaults, validates values at startup, and supports multiple environments. I want config that fails fast on misconfiguration and never silently uses wrong values. Assume twelve-factor style config in 2026. ## ROLE Act as a Go backend engineer who treats configuration as a first-class, validated input. You prefer environment variables, validate at startup, and make missing or invalid config fail loudly before serving traffic. ## RESPONSE GUIDELINES - Load configuration primarily from environment variables. - Validate all values at startup and fail fast on errors. - Provide clear defaults and document every setting. - Keep secrets out of code and logs. ## TASK CRITERIA ### Define The Config Struct - Model configuration as a typed struct with clear fields. - Group related settings such as server, database, and timeouts. - Use appropriate types so parsing happens once at load. - Document each field and its unit where relevant. ### Load From The Environment - Read each value from a named environment variable. - Apply defaults for optional settings explicitly. - Parse durations, sizes, and booleans into proper types. - Support an optional dotenv file for local development only. ### Validate Thoroughly - Require all mandatory settings and report missing ones together. - Check ranges, formats, and mutually dependent fields. - Fail startup with a clear, aggregated error message. - Never start serving traffic with invalid configuration. ### Handle Secrets Safely - Read secrets from the environment or a secret manager. - Avoid printing secret values in logs or errors. - Support secret rotation without code changes where possible. - Mark sensitive fields so they are redacted in dumps. ### Support Multiple Environments - Drive environment differences through env vars, not code branches. - Provide a way to print the effective config with secrets redacted. - Document the variables needed per environment. - Keep the same binary across dev, staging, and production. ## ASK THE USER FOR - The settings your service needs and which are required. - Where secrets come from such as a vault or platform secrets. - The environments you deploy to and their differences. - Whether local development needs a dotenv workflow.
Or press ⌘C to copy