config

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package config provides a first-class configuration loader that binds environment variables, config files, and secret sources into typed Go structs with validation.

Apps currently roll their own os.Getenv calls. This package removes that class of bugs with a single typed binding step.

Usage:

type AppConfig struct {
    Port    int    `config:"PORT" default:"8080" validate:"min=1,max=65535"`
    DBURL   string `config:"DATABASE_URL" required:"true"`
    Debug   bool   `config:"DEBUG" default:"false"`
    LogLevel string `config:"LOG_LEVEL" default:"info" validate:"oneof=debug info warn error"`
}

var cfg AppConfig
if err := config.Load(&cfg); err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(cfg interface{}, sources ...Source) error

Load populates the config struct from the given sources (defaulting to EnvSource if none are provided). Struct fields use `config:"KEY"` tags to specify the source key, `default:"VALUE"` for defaults, and `required:"true"` for mandatory fields.

Nested struct fields recurse with a SCREAMING_SNAKE prefix derived from the parent field name (e.g. `DB DBConfig` reads keys like DB_HOST).

Fields tagged `sensitive:"true"` have their raw value redacted from any error messages.

If the config (or a pointer to it) implements ConfigValidator, Validate is called after binding and its error is returned.

Supported field types: string, int, int64, float64, bool, Duration, and nested structs.

func LoadWith

func LoadWith(cfg interface{}, sources ...Source) error

LoadWith is an alias for Load. Populates config from sources.

func MustLoad

func MustLoad(cfg interface{}, sources ...Source)

MustLoad is like Load but panics on error. Use in init() or main().

Types

type ChainedSource

type ChainedSource []Source

ChainedSource tries multiple sources in order, returning the first hit.

func (ChainedSource) Get

func (cs ChainedSource) Get(key string) (string, bool)

Get tries each source in order.

type ConfigValidator

type ConfigValidator interface {
	Validate() error
}

ConfigValidator is implemented by config structs that want a post-binding validation hook. Validate runs after every field has been populated; returning a non-nil error aborts Load.

Renamed from Validator to avoid collision with entity.Entity's own Validate() error method — a config struct that doubled as an entity would otherwise accidentally satisfy this interface.

type EnvSource

type EnvSource struct{}

EnvSource reads from environment variables. This is the default source.

func (EnvSource) Get

func (EnvSource) Get(key string) (string, bool)

Get returns the environment variable for the given key. Distinguishes between unset (returns ok=false) and set-but-empty (returns "", true). Conflating those breaks defaulting — an explicit empty value used to silently fall back to the `default:` tag.

type MapSource

type MapSource map[string]string

MapSource reads from a static map. Useful for testing.

func (MapSource) Get

func (m MapSource) Get(key string) (string, bool)

Get returns the value from the map.

type Source

type Source interface {
	// Get returns the value for the given key, or ("", false) if not found.
	Get(key string) (string, bool)
}

Source provides configuration values. The default source reads from environment variables. Custom sources (files, secret managers, etc.) implement this interface.

Jump to

Keyboard shortcuts

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