Documentation
¶
Index ¶
- Variables
- func Export() map[string]string
- func ExportWithPrefix(prefix string) map[string]string
- func Load(cfg any) error
- func LoadDotEnv(path string, override ...bool) error
- func LoadDotEnvOverride(path string) errordeprecated
- func LoadWithPrefix(cfg any, prefix string) error
- func MustLoad(cfg any)
- func MustLoadDotEnv(path string)
- func MustLoadWithPrefix(cfg any, prefix string)
- func RegisterParser[T any](parser func(string) (T, error))
- func Set(key, value string)
- func Unset(key string)
- type Loader
- type MultiError
- type ParseError
- type RequiredError
- type ValidationError
- type Var
Constants ¶
This section is empty.
Variables ¶
var DefaultLoader = NewLoader()
DefaultLoader is the default loader instance used by global functions.
Functions ¶
func ExportWithPrefix ¶
ExportWithPrefix returns environment variables matching a prefix.
func Load ¶
Load populates a struct from environment variables using struct tags.
Supported tags:
- env:"VAR_NAME" - the environment variable name
- default:"value" - default value if not set
- required:"true" - marks the field as required
Example:
type Config struct {
Port int `env:"PORT" default:"8080"`
Debug bool `env:"DEBUG" default:"false"`
Database string `env:"DATABASE_URL" required:"true"`
Timeout time.Duration `env:"TIMEOUT" default:"30s"`
}
func LoadDotEnv ¶
LoadDotEnv loads environment variables from a .env file. By default, it does NOT override existing environment variables. Pass true as the second argument to override existing variables.
Examples:
LoadDotEnv(".env") // doesn't override existing vars
LoadDotEnv(".env", false) // same as above
LoadDotEnv(".env", true) // overrides existing vars
func LoadDotEnvOverride
deprecated
func LoadWithPrefix ¶
LoadWithPrefix populates a struct with a prefix for all env vars. For example, LoadWithPrefix(cfg, "APP") will look for APP_PORT instead of PORT.
func MustLoadWithPrefix ¶
MustLoadWithPrefix is like LoadWithPrefix but panics on error.
func RegisterParser ¶
RegisterParser registers a custom parser for a specific type T. This parser will be used when loading structs with fields of type T.
Types ¶
type Loader ¶ added in v0.3.0
type Loader struct {
// contains filtered or unexported fields
}
Loader manages the configuration loading and parser registry.
func NewLoader ¶ added in v0.3.0
func NewLoader() *Loader
NewLoader creates a new Loader with default parsers registered.
func (*Loader) Load ¶ added in v0.3.0
Load populates a struct from environment variables using struct tags.
func (*Loader) LoadWithPrefix ¶ added in v0.3.0
LoadWithPrefix populates a struct with a prefix for all env vars.
func (*Loader) RegisterParser ¶ added in v0.3.0
RegisterParser registers a custom parser for a specific type T on this Loader instance.
type MultiError ¶
type MultiError struct {
Errors []error
}
MultiError contains multiple errors from struct loading.
func (*MultiError) Error ¶
func (e *MultiError) Error() string
func (*MultiError) Unwrap ¶
func (e *MultiError) Unwrap() []error
Unwrap returns the list of errors.
type ParseError ¶
ParseError is returned when an environment variable cannot be parsed.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
func (*ParseError) Unwrap ¶
func (e *ParseError) Unwrap() error
Unwrap returns the underlying error.
type RequiredError ¶
type RequiredError struct {
Key string
}
RequiredError is returned when a required environment variable is not set.
func (*RequiredError) Error ¶
func (e *RequiredError) Error() string
type ValidationError ¶
ValidationError is returned when struct validation fails.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type Var ¶
type Var[T any] struct { // contains filtered or unexported fields }
Var represents an environment variable with type-safe access.
func NewVar ¶ added in v0.3.0
NewVar creates a new environment variable of type T using the specified Loader. It searches for a registered parser or uses encoding.TextUnmarshaler.
func WithLoader ¶ added in v0.3.0
WithLoader creates a new environment variable of type T using the specified Loader. This provides a more fluent API for creating variables with isolated loaders.
Example:
loader := dotenvgo.NewLoader()
loader.RegisterParser(func(s string) (MyType, error) { ... })
value := dotenvgo.WithLoader[MyType](loader, "MY_VAR").Get()
func (*Var[T]) Get ¶
func (v *Var[T]) Get() T
Get returns the value of the environment variable. Panics if the variable is required but not set.
func (*Var[T]) MustGet ¶
func (v *Var[T]) MustGet() T
MustGet returns the value or panics if there's an error. Alias for Get().
func (*Var[T]) Required ¶
Required marks the environment variable as required. Get() will panic if the variable is not set. GetE() will return an error.
func (*Var[T]) WithPrefix ¶
WithPrefix adds a prefix to the environment variable key. For example, WithPrefix("APP").String("PORT") will look for "APP_PORT".
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
expansion
command
|
|
|
file
command
|
|
|
isolated_loader
command
Package main demonstrates the isolated loader feature of dotenvgo.
|
Package main demonstrates the isolated loader feature of dotenvgo. |
|
struct
command
Example: Struct-based configuration loading
|
Example: Struct-based configuration loading |