Documentation
¶
Overview ¶
Package configkit provides environment variable configuration loading with automatic .env file support and configurable prefixes.
This package wraps vendored copies of github.com/caarlos0/env and github.com/joho/godotenv with sensible defaults for Beaver applications.
12-Factor Compliance ¶
configkit follows the III. Config principle of the 12-factor app methodology (https://12factor.net/config): configuration is read strictly from the environment, never compiled in or stored in version-controlled config files. The optional .env loader is a developer-ergonomics layer only — process environment variables always take precedence over file values, so deployment platforms (Kubernetes, systemd, CI runners, secret managers) remain the source of truth in production.
Value precedence, highest to lowest:
- Process environment variables
- Earlier .env files listed in WithEnvFiles
- Later .env files
- envDefault struct tags
Basic Usage ¶
type Config struct {
Host string `env:"HOST" envDefault:"localhost"`
Port int `env:"PORT" envDefault:"8080"`
}
var cfg Config
configkit.Load(&cfg) // Uses BEAVER_ prefix, loads .env
Environment Variables ¶
By default, all environment variables are prefixed with "BEAVER_":
BEAVER_HOST=example.com BEAVER_PORT=3000
Custom Prefixes ¶
Use WithPrefix for multi-instance configurations:
// Primary database: PRIMARY_DB_HOST, PRIMARY_DB_PORT
configkit.Load(&dbCfg, configkit.WithPrefix("PRIMARY_DB_"))
// Replica database: REPLICA_DB_HOST, REPLICA_DB_PORT
configkit.Load(&dbCfg, configkit.WithPrefix("REPLICA_DB_"))
// No prefix: DB_HOST, DB_PORT
configkit.Load(&dbCfg, configkit.WithPrefix(""))
Supported Types ¶
All types supported by caarlos0/env are available:
type Config struct {
// Basic types
String string `env:"STRING"`
Int int `env:"INT"`
Bool bool `env:"BOOL"`
Duration time.Duration `env:"DURATION"`
// Slices
Hosts []string `env:"HOSTS" envSeparator:","`
// Required fields
APIKey string `env:"API_KEY,required"`
// Defaults
Port int `env:"PORT" envDefault:"8080"`
// Nested structs
Database DatabaseConfig `envPrefix:"DB_"`
}
.env File Support ¶
The package automatically loads .env files before parsing. Use WithEnvFiles to specify custom files:
configkit.Load(&cfg, configkit.WithEnvFiles(".env", ".env.local"))
Use WithoutDotEnv to disable automatic loading:
configkit.Load(&cfg, configkit.WithoutDotEnv())
Multi-Instance Pattern ¶
The prefix pattern enables running multiple instances with different configs:
// Environment:
// DEV_SLACK_WEBHOOK_URL=https://hooks.slack.com/dev
// PROD_SLACK_WEBHOOK_URL=https://hooks.slack.com/prod
devSlack := slack.WithPrefix("DEV_").New()
prodSlack := slack.WithPrefix("PROD_").New()
This pattern avoids YAML complexity while remaining 12-factor compliant.
Index ¶
Constants ¶
const DefaultPrefix = "BEAVER_"
DefaultPrefix is the default environment variable prefix.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load parses environment variables into a struct. Automatically loads .env files first (can be disabled via options).
A missing .env file is not an error. A malformed one is.
Example:
type Config struct {
Host string `env:"HOST" envDefault:"localhost"`
Port int `env:"PORT" envDefault:"8080"`
}
var cfg Config
configkit.Load(&cfg) // Uses BEAVER_ prefix
configkit.Load(&cfg, configkit.WithPrefix("")) // No prefix
configkit.Load(&cfg, configkit.WithPrefix("APP_")) // Custom prefix
Types ¶
type Option ¶
type Option func(*Options)
Option configures Load behavior.
func WithEnvFiles ¶
WithEnvFiles sets custom .env files to load.
Files are loaded with first-wins semantics: process env always takes precedence, then earlier files override later ones. To make a local override file win over a base file, list the override file first:
configkit.Load(&cfg, configkit.WithEnvFiles(".env.local", ".env"))
func WithPrefix ¶
WithPrefix sets a custom environment variable prefix.
func WithRequired ¶
func WithRequired() Option
WithRequired makes all fields required unless they have defaults.
func WithoutDotEnv ¶
func WithoutDotEnv() Option
WithoutDotEnv disables automatic .env file loading.
type Options ¶
type Options struct {
// Prefix for environment variable names (default: "BEAVER_").
Prefix string
// EnvFiles to load before parsing (default: ".env"). Earlier entries
// take precedence over later ones. See WithEnvFiles for the rationale.
EnvFiles []string
// SkipDotEnv skips loading .env files entirely. Useful in tests and
// in environments where all configuration comes from the process env.
SkipDotEnv bool
// Required makes all fields required unless they have envDefault tags.
// Equivalent to setting `,required` on every field.
Required bool
}
Options configures the config loader.
Value precedence (highest to lowest):
- Process environment variables (set by the OS, container, shell, etc.)
- Earlier entries in EnvFiles (first file wins on conflict)
- Later entries in EnvFiles
- envDefault tag values on the target struct
Process env always wins over file contents — this matches 12-factor app conventions and lets deployment platforms (Kubernetes, systemd, CI) override committed defaults without editing files.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package godotenv is a go port of the ruby dotenv library (https://github.com/bkeepers/dotenv)
|
Package godotenv is a go port of the ruby dotenv library (https://github.com/bkeepers/dotenv) |
|
Package env is a simple, zero-dependencies library to parse environment variables into structs.
|
Package env is a simple, zero-dependencies library to parse environment variables into structs. |