Documentation
¶
Overview ¶
Package env loads dotenv files and process environment variables into nested maps and Go structs. Multiple sources merge with later scalars overriding earlier ones; maps merge recursively.
Example:
c := env.New(
env.WithFile(".default.env"),
env.WithFile(".env"),
env.WithCurrentEnvironment(),
)
var cfg MyConfig
if err := c.Unmarshal(&cfg); err != nil { ... }
Index ¶
- type Codec
- type Option
- func WithBytes(b []byte) Option
- func WithCurrentEnvironment() Option
- func WithDecodeHook(h mapstructure.DecodeHookFunc) Option
- func WithFile(path string) Option
- func WithHTTPClient(client *http.Client) Option
- func WithHTTPHeader(k, v string) Option
- func WithKeyNormalizer(n keymap.Normalizer) Option
- func WithPrefix(prefix string) Option
- func WithReader(r io.Reader) Option
- func WithSliceMerge(s merge.SliceStrategy) Option
- func WithTagName(name string) Option
- func WithTrim(enable bool) Option
- func WithURL(raw string) Option
- func WithWeaklyTyped(enable bool) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec struct {
// contains filtered or unexported fields
}
Codec loads environment-style key/value data from multiple sources.
func New ¶
New builds a Codec from options. Sources are merged left-to-right; later sources override earlier scalar leaves (see package merge).
func (*Codec) Map ¶
Map returns the merged nested map after all sources are loaded and keys normalized.
func (*Codec) Marshal ¶
Marshal encodes src into dotenv-style bytes (KEY=value lines, sorted keys). src may be a struct or map[string]any.
func (*Codec) UnmarshalContext ¶
UnmarshalContext decodes the merged map into dst using mapstructure.
type Option ¶
type Option func(*Codec)
Option configures a Codec.
func WithCurrentEnvironment ¶
func WithCurrentEnvironment() Option
WithCurrentEnvironment appends the process environment as a source (read at Map time).
func WithDecodeHook ¶
func WithDecodeHook(h mapstructure.DecodeHookFunc) Option
WithDecodeHook appends a decode hook.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used by subsequent WithURL sources.
func WithHTTPHeader ¶
WithHTTPHeader adds a header for subsequent WithURL sources.
func WithKeyNormalizer ¶
func WithKeyNormalizer(n keymap.Normalizer) Option
WithKeyNormalizer sets key normalizer after merge (nil disables).
func WithPrefix ¶
WithPrefix strips prefix from variable names before path splitting.
func WithReader ¶
WithReader appends dotenv content from r (read fully on each Map call).
func WithSliceMerge ¶
func WithSliceMerge(s merge.SliceStrategy) Option
WithSliceMerge sets slice merge strategy when merging sources.
func WithTagName ¶
WithTagName sets the struct tag name for mapstructure.
func WithURL ¶
WithURL appends an HTTP(S) URL returning dotenv content. Use WithHTTPHeader / WithHTTPClient before WithURL so they apply to this request.
func WithWeaklyTyped ¶
WithWeaklyTyped toggles weak typing for struct decode.