config

package
v1.11.9 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package config loads typed application configuration from explicit sources. New applications should use Loader to avoid process-global mutation and must obtain production secrets from an appropriate secret manager.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(target any, filepath ...string) error

Bind is a convenient alias for LoadAndUnmarshal.

func Env

func Env(key string, defaultValue ...string) string

Env is a shorthand alias for GetString.

func Get

func Get(key string, defaultValue ...string) string

Get is a shorthand alias for GetString.

func GetBool

func GetBool(key string, defaultValue ...bool) bool

GetBool retrieves a boolean config value.

func GetDuration

func GetDuration(key string, defaultValue ...time.Duration) time.Duration

GetDuration retrieves a time.Duration config value (e.g. "10s", "5m").

func GetInt

func GetInt(key string, defaultValue ...int) int

GetInt retrieves an integer config value.

func GetString

func GetString(key string, defaultValue ...string) string

GetString retrieves a string config value from environment or loaded .env file.

func IntRange

func IntRange(field string, value, minimum, maximum int64) error

IntRange validates an integer against inclusive bounds.

func Load

func Load(filepath ...string) error

Load parses a .env file and loads key-value pairs into the environment store.

func LoadAndUnmarshal

func LoadAndUnmarshal(target any, filepath ...string) error

LoadAndUnmarshal parses a .env file and unmarshals environment variables into the target struct pointer.

func LoadStruct

func LoadStruct[T any](filepath ...string) (*T, error)

LoadStruct parses a .env file and populates a new struct pointer of type T.

func LoadValue

func LoadValue[T any](loader *Loader, normalize func(*T) error, validate func(T) error) (T, error)

LoadValue returns a populated value only after normalization and validation succeed, avoiding exposure of partially loaded configuration.

func MaskSecret

func MaskSecret(secret string) string

MaskSecret redacts sensitive credentials (e.g. passwords, AWS keys) for logging safety.

func MustLoad

func MustLoad[T any](filepath ...string) *T

MustLoad parses a .env file and populates a new struct of type T, panicking on unmarshal failure.

func OneOf

func OneOf(field, value string, allowed ...string) error

OneOf validates that value exactly matches an allowed string.

func Port

func Port(field string, value int) error

Port validates an integer TCP or UDP port number.

func Required

func Required(field, value string) error

Required rejects an empty or whitespace-only configuration value.

func StringLength

func StringLength(field, value string, minimum, maximum int) error

StringLength validates a configuration string using Unicode code-point length.

func Unmarshal

func Unmarshal(v any) error

Unmarshal maps environment variables into a struct pointer using `env:"KEY"`, `config:"KEY"`, `default:"VALUE"`, `required:"true"` tags or automatic SNAKE_CASE field names.

Types

type Loader

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

Loader is an immutable, process-environment-independent configuration source.

func NewLoader

func NewLoader(options LoaderOptions) (*Loader, error)

NewLoader constructs an immutable loader; environment values override dotenv values.

func (*Loader) Load

func (loader *Loader) Load(target any, normalize func() error, validate func() error) error

Load populates target, then runs optional normalization and validation callbacks.

type LoaderOptions

type LoaderOptions struct {
	Environment    map[string]string
	DotEnvPath     string
	OptionalDotEnv bool
}

LoaderOptions selects explicit environment values and an optional dotenv file.

Jump to

Keyboard shortcuts

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