Skip to content

Examples ​

The env library ships 11 runnable examples in the examples/ directory of the source repository. Each example is a self-contained main program, covering everything from global-mode basics to security features and advanced capabilities. All examples are included in normal builds (go build ./...).

Running the Examples ​

Clone the repository at 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 reference shared configuration files under examples/data/ using relative paths, so they must be run from the project root for those paths to resolve.

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 capturing read, write, 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

Reading Order ​

We recommend reading the examples in numeric order: 01–03 cover the global and instance modes plus typed access, 04–07 introduce production features such as struct mapping, secure values, and auditing, and 08–11 move on to utilities, error handling, and advanced features. Every example can be verified directly with go run and pairs well with its linked documentation page.