config

package
v0.64.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package config provides a stdlib-only, dependency-light configuration seam. Core packages use ONLY reflect, strconv, time, os, flag, encoding/json, bufio, strings — NO third-party imports. Heavy adapters (YAML, TOML, remote) live in sub-packages (e.g. config/koanf) and are opt-in.

Usage:

var opts config.ServerOptions
if err := config.Load(&opts,
    config.Flags(fs),
    config.Env("MY_SVC_"),
    config.DotEnv(".env"),
); err != nil {
    log.Fatal(err)
}

Precedence (highest to lowest): sources listed first win, then default tag.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(dst any, sources ...Source) error

Load populates the exported, tagged fields of dst (must be a non-nil pointer to a struct) from sources in precedence order: the first source that returns ok=true for a key wins. If no source provides a value and a `default:` tag is present, that default is used. Fields without a `config:` tag are skipped.

Supported field kinds: string, int, int64, bool, float64, time.Duration. Any other kind returns an error naming the key.

Types

type DotEnvSource

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

DotEnvSource lazily reads a .env file on first use.

func (*DotEnvSource) Get

func (s *DotEnvSource) Get(key string) (string, bool)

type EnvSource

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

EnvSource looks up keys in the environment with an optional prefix.

func (*EnvSource) Get

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

type FlagsSource

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

FlagsSource reads from a *flag.FlagSet, returning values only for flags that were explicitly Set on the command line (not just their defaults).

func (*FlagsSource) Get

func (s *FlagsSource) Get(key string) (string, bool)

type JSONFileSource

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

JSONFileSource reads a flat JSON object from a file.

func (*JSONFileSource) Get

func (s *JSONFileSource) Get(key string) (string, bool)

type MapSource

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

MapSource is a simple in-memory map source, useful for testing or overrides.

func (*MapSource) Get

func (s *MapSource) Get(key string) (string, bool)

type ServerOptions

type ServerOptions struct {
	// GRPCAddr is the TCP listen address for the gRPC endpoint.
	GRPCAddr string `config:"GRPC_ADDR" default:":9090"`
	// HTTPAddr is the TCP listen address for the HTTP/JSON gateway.
	// Empty disables the gateway.
	HTTPAddr string `config:"HTTP_ADDR" default:":8080"`
	// LogLevel is the minimum log level (debug, info, warn, error).
	LogLevel string `config:"LOG_LEVEL" default:"info"`
	// OTLPEndpoint is the OpenTelemetry collector endpoint (host:port).
	// Empty means honor OTEL_EXPORTER_OTLP_ENDPOINT or no-op.
	OTLPEndpoint string `config:"OTLP_ENDPOINT" default:""`
	// DSN is the database connection string.
	// Empty means the service manages its own default (e.g. in-memory sqlite).
	DSN string `config:"DSN" default:""`
}

ServerOptions holds the canonical per-service configuration that the SDK scaffold loads through config.Load. Fields are tagged with their lookup key and a sane default.

The scaffold loads this with:

var opts ServerOptions
config.Load(&opts, config.Flags(fs), config.Env("MYSVC_"), config.DotEnv(".env"))

type Source

type Source interface {
	Get(key string) (value string, ok bool)
}

Source is the single abstraction over any config origin (env, flags, file, map, remote). Get returns the string value for key and whether it was present. Implementations must be safe for concurrent use.

func DotEnv

func DotEnv(path string) Source

DotEnv returns a Source that reads from a .env file at path. Blank lines and lines starting with # are ignored. If the file does not exist, the source is silently empty.

func Env

func Env(prefix string) Source

Env returns a Source that reads from environment variables. The key is looked up as prefix+key (e.g. prefix "APP_" + key "GRPC_ADDR" → "APP_GRPC_ADDR").

func Flags

func Flags(fs *flag.FlagSet) Source

Flags returns a Source backed by fs. A key matches a flag when the flag name equals the key or its lowercase equivalent.

func JSONFile

func JSONFile(path string) Source

JSONFile returns a Source backed by a JSON file at path. The file must contain a JSON object; values are coerced to strings. If the file does not exist, the source is silently empty.

func Map

func Map(m map[string]string) Source

Map returns a Source backed by m.

Directories

Path Synopsis
koanf module

Jump to

Keyboard shortcuts

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