pkg/env¶
Helpers for reading configuration from environment variables with sensible defaults and explicit “must” variants that panic when a value is missing or malformed.
Quick Start¶
API Cheatsheet¶
| Function | Purpose |
|---|---|
EnvOrDefault(key, fallback string) |
string with fallback |
MustEnv(key) |
string or panic if empty |
EnvAsInt(key, fallback int) / MustEnvAsInt(key) |
parse integer values |
EnvAsBool(key, fallback bool) / MustEnvAsBool(key) |
parse boolean values |
Each helper treats “missing” as "" and panics with descriptive messages for invalid conversions.
Examples¶
Strings¶
Integers¶
Booleans¶
Tips¶
- Use
EnvAs*when you can tolerate defaults (local dev) andMustEnv*for production-critical knobs. - Panics happen on invalid formats (e.g.,
EnvAsInt("PORT")withPORT=abc). Keep these calls near bootstrapping code so the service fails fast. - Wrap lookups in a struct constructor (see Quick Start) to centralize configuration logic.