command
Version:
v0.1.2
Opens a new window with list of versions in this module.
Published: Jul 18, 2025
License: MIT
Opens a new window with license information.
Imports: 3
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
Example 02: Environment Variables
Loading configuration from environment variables with prefix handling.
What This Shows
- Using
FromEnv to load environment variables
- Prefix stripping (MYAPP_ prefix is removed during matching)
- Layer precedence (environment overrides defaults)
- How non-prefixed variables are ignored
Running
go run main.go
Key Points
- FromEnv - Creates a layer from environment variables
- Prefix Stripping -
MYAPP_SERVER_PORT matches server.port
- Precedence - Later layers (environment) override earlier ones (defaults)
- Selective Loading - Only variables with the prefix are included
Environment Variable Matching
The library automatically matches environment variables to configuration keys:
MYAPP_SERVER_PORT → server.port
MYAPP_DATABASE_HOST → database.host
MYAPP_FEATURE_BETA → feature.beta
Output Example
=== Layer Contents ===
Layer: defaults (prefix: "")
server.port = 8080
server.host = localhost
database.host = localhost
...
Layer: environment (prefix: "MYAPP_")
MYAPP_SERVER_PORT = 9000
MYAPP_DATABASE_HOST = db.production.com
...
=== Resolved Configuration ===
server.port = 9000 (from environment)
server.host = localhost (from defaults)
database.host = db.production.com (from environment)
...
Documentation
¶
There is no documentation for this package.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.