Examples
The source repository ships 13 runnable examples under examples/, numbered 01 to 13. Each is a self-contained main program, and all of them are included in go build ./....
Running the Examples
Clone github.com/cybergodev/env, then run go run from the project root:
git clone https://github.com/cybergodev/env.git
cd env
go run ./examples/01_quickstart/Examples 01-06, 08, and 10-12 read shared configuration files from examples/data/ through relative paths, so run them from the project root; from another directory the data files are not found — most examples fall back to default values, while examples 02 and 11 fail outright. Examples 07, 09, and 13 inline their data and run from any directory.
Shared Data Files
| File | Format | Purpose |
|---|---|---|
config.env | dotenv | Flat KEY=value pairs, variable expansion demo |
config.json | JSON | Nested structure (app/db/cache/features) |
config.yaml | YAML | Same structure as JSON, for struct mapping demos |
Example Index
| # | Directory | What It Demonstrates | Related Docs |
|---|---|---|---|
| 01 | 01_quickstart/ | Global-mode basics: Load initialization, typed access via GetString/GetInt/GetBool/GetDuration, Lookup existence checks, runtime Set | Quick Start |
| 02 | 02_loader_config/ | Instance mode with four config presets (Default/Development/Production/Testing), custom configs, and explicit lifecycle control | Config, Loader |
| 03 | 03_type_access/ | Every typed getter: string, int, bool, duration, float, uint, and slice, plus Lookup, including dot-notation paths | Quick Start |
| 04 | 04_struct_mapping/ | Struct mapping with env tags: nested structs, envDefault defaults, and marshaling | Struct Mapping |
| 05 | 05_secure_values/ | SecureValue lifecycle: Close vs Release, memory zeroing, and mlock memory locking | Memory Locking, SecureValue |
| 06 | 06_audit_logging/ | JSON, Channel, and Log audit handlers, automatically recording load, set, and delete events | Audit Logging |
| 07 | 07_marshal_unmarshal/ | Format conversion: map ↔ string, struct ↔ map, multi-format output, and round trips | Serialization |
| 08 | 08_utilities/ | Utility functions: introspection (Len/Keys/All), key deletion, masking, and format detection | Multi-format Config |
| 09 | 09_error_handling/ | Sentinel errors (errors.Is) and typed errors (errors.As), including missing-file and closed-loader cases | Error Handling |
| 10 | 10_concurrency/ | Concurrent reads, writes, and mixed access, demonstrating shard-lock concurrency safety | Concurrency |
| 11 | 11_advanced/ | Variable expansion ($VAR and ${VAR}), multiple file loading with override, and prefix filtering | Variable Expansion |
| 12 | 12_extensibility/ | Registering a custom parser with RegisterParser, interface-segregated consumption via EnvGetter (dependency injection and test doubles), and custom components (CustomAuditor counting audit events) | Custom Parser, Interfaces |
| 13 | 13_customization/ | Custom Validator/Expander/FileSystem, fail-closed auditing (ErrAuditFailed), custom Marshaler/Unmarshaler, and brace operators | Interfaces, Audit Logging, Serialization |
Reading Order
The examples build on each other in numeric order. 01–03 cover global and instance mode plus typed access. 04–07 introduce struct mapping, secure values, auditing, and serialization. 08–13 cover utilities, error handling, concurrency, variable expansion, and extensibility. Each example runs directly with go run and links to its documentation page above.