Documentation
¶
Overview ¶
Package main demonstrates config.FromEnv: loading application configuration exclusively from environment variables using the codec as the single source of truth for field names, types, validations, and documentation.
No config file is involved. The codec's schema drives type coercion so that env var strings are converted to the correct Go types before decoding.
One codec, all input formats — the same configCodec defined below is used for every config.FromEnv call. Whether env vars are plain strings ("8080"), comma-separated lists ("web,api"), or full JSON objects/arrays, config.FromEnv parses the raw string and builds an intermediate map[string]any, then calls configCodec.Decode — the exact same Decode path used by format.JSON, format.TOML, and format.YAML. No special codec is needed for JSON-formatted env vars.
Naming convention: strings.ToUpper(prefix + field_name)
field "port" + prefix "APP_" → APP_PORT field "log_level" + prefix "APP_" → APP_LOG_LEVEL field "db" (nested) + prefix "APP_" → APP_DB_ prefix for its sub-fields nested field "host" → APP_DB_HOST
The example also shows validate.EnvVarName and validate.EnvVarPrefix for validating user-supplied env var names before passing them to FromEnvVar.
Run with: go run ./examples/env-config