Documentation
¶
Index ¶
- func Export() map[string]string
- func ExportWithPrefix(prefix string) map[string]string
- func Load(cfg any) error
- func LoadDotEnv(path string) error
- func LoadDotEnvOverride(path string) error
- 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 MultiError
- type ParseError
- type RequiredError
- type ValidationError
- type Var
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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. It does NOT override existing environment variables.
func LoadDotEnvOverride ¶
LoadDotEnvOverride loads environment variables from a .env file. It DOES override existing environment variables.
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 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 New ¶
New creates a new environment variable of type T. It searches for a registered parser or uses encoding.TextUnmarshaler.
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".