05-layering

command
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Example 05: Configuration Layering

Demonstrates a real-world configuration hierarchy with multiple sources.

What This Shows

  • Multiple configuration layers with clear precedence
  • Loading from different sources (defaults, files, env, flags)
  • How later layers override earlier ones
  • Tracing values to their source
  • Command-line flag integration

Running

# Basic run (uses defaults and environment)
go run main.go

# Override with flags
go run main.go --port=3000 --debug --workers=8

# Set environment variables
APP_SERVER_PORT=9000 APP_DEBUG=true go run main.go

Configuration Hierarchy

  1. Defaults - Built-in fallback values
  2. Config File - Application configuration (simulated)
  3. Environment - Deployment-specific settings (APP_* prefix)
  4. Command Flags - Runtime overrides (highest priority)

Key Points

  1. Layer Order - Later layers override earlier ones
  2. Selective Override - Only specified values are overridden
  3. Source Tracking - Every value can be traced to its layer
  4. Flag Integration - Command-line flags as the final layer
  5. Prefix Handling - Environment variables with prefix stripping

Precedence Example

If server.port is defined in multiple layers:

  • defaults: "8080"
  • config.json: (not defined)
  • environment: "9000" (from APP_SERVER_PORT)
  • flags: "3000" (from --port=3000)

The final value will be "3000" (from flags).

Use Cases

This pattern is ideal for:

  • Applications deployed in multiple environments
  • Services that need runtime configuration
  • Tools that combine config files with CLI options
  • Debugging configuration issues (clear source tracking)

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL