config

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package config provides environment and file-based configuration with type-safe accessors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Env

func Env(key, def string) string

Env returns an env variable by name, falling back to def.

func EnvRequired

func EnvRequired(key string) string

EnvRequired returns an env variable or panics if absent.

func Load

func Load(cfg *Config, path string) error

Load reads a config file (YAML or TOML) and merges it into cfg. The format is inferred from the file extension.

func LoadDir

func LoadDir(cfg *Config, dir string) error

LoadDir reads all YAML/TOML files in dir and merges them using the filename (without extension) as the top-level key. E.g. config/database.yaml → "database.*".

func LoadEnv

func LoadEnv(path string) error

LoadEnv parses a .env file and sets each key=value pair as an environment variable. Existing OS env variables are NOT overwritten (use LoadEnvOverride for that).

func LoadEnvOverride

func LoadEnvOverride(path string) error

LoadEnvOverride parses a .env file and overwrites existing OS env variables.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config holds the merged configuration from all sources (env, YAML, TOML, .env files).

func New

func New() *Config

New returns an empty Config.

func (*Config) All

func (c *Config) All() map[string]any

All returns a shallow copy of the top-level map.

func (*Config) Bool

func (c *Config) Bool(key string, def bool) bool

Bool returns the value at key as a bool, falling back to def.

func (*Config) Duration

func (c *Config) Duration(key string, def time.Duration) time.Duration

Duration returns the value at key as time.Duration (parsed from string e.g. "5m", "1h").

func (*Config) Get

func (c *Config) Get(key string) any

Get returns the raw value at key, or nil if not found.

func (*Config) GetD

func (c *Config) GetD(key string, def any) any

GetD returns the value at key, falling back to def if absent.

func (*Config) Int

func (c *Config) Int(key string, def int) int

Int returns the value at key as an int, falling back to def.

func (*Config) Merge

func (c *Config) Merge(values map[string]any)

Merge merges a map of values into the configuration (dot-notation keys).

func (*Config) Set

func (c *Config) Set(key string, value any)

Set stores a value at the given dot-notation key (e.g. "database.host").

func (*Config) String

func (c *Config) String(key, def string) string

String returns the value at key as a string, falling back to def.

Jump to

Keyboard shortcuts

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