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 ¶
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.
type EnvSource ¶
type EnvSource struct {
// contains filtered or unexported fields
}
EnvSource looks up keys in the environment with an optional prefix.
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).
type JSONFileSource ¶
type JSONFileSource struct {
// contains filtered or unexported fields
}
JSONFileSource reads a flat JSON object from a file.
type MapSource ¶
type MapSource struct {
// contains filtered or unexported fields
}
MapSource is a simple in-memory map source, useful for testing or overrides.
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 ¶
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 ¶
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 ¶
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 ¶
Flags returns a Source backed by fs. A key matches a flag when the flag name equals the key or its lowercase equivalent.