Skip to content

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:

bash
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 ​

FileFormatPurpose
config.envdotenvFlat KEY=value pairs, variable expansion demo
config.jsonJSONNested structure (app/db/cache/features)
config.yamlYAMLSame structure as JSON, for struct mapping demos

Example Index ​

#DirectoryWhat It DemonstratesRelated Docs
0101_quickstart/Global-mode basics: Load initialization, typed access via GetString/GetInt/GetBool/GetDuration, Lookup existence checks, runtime SetQuick Start
0202_loader_config/Instance mode with four config presets (Default/Development/Production/Testing), custom configs, and explicit lifecycle controlConfig, Loader
0303_type_access/Every typed getter: string, int, bool, duration, float, uint, and slice, plus Lookup, including dot-notation pathsQuick Start
0404_struct_mapping/Struct mapping with env tags: nested structs, envDefault defaults, and marshalingStruct Mapping
0505_secure_values/SecureValue lifecycle: Close vs Release, memory zeroing, and mlock memory lockingMemory Locking, SecureValue
0606_audit_logging/JSON, Channel, and Log audit handlers, automatically recording load, set, and delete eventsAudit Logging
0707_marshal_unmarshal/Format conversion: map ↔ string, struct ↔ map, multi-format output, and round tripsSerialization
0808_utilities/Utility functions: introspection (Len/Keys/All), key deletion, masking, and format detectionMulti-format Config
0909_error_handling/Sentinel errors (errors.Is) and typed errors (errors.As), including missing-file and closed-loader casesError Handling
1010_concurrency/Concurrent reads, writes, and mixed access, demonstrating shard-lock concurrency safetyConcurrency
1111_advanced/Variable expansion ($VAR and ${VAR}), multiple file loading with override, and prefix filteringVariable Expansion
1212_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
1313_customization/Custom Validator/Expander/FileSystem, fail-closed auditing (ErrAuditFailed), custom Marshaler/Unmarshaler, and brace operatorsInterfaces, 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.