dotenvgo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Export

func Export() map[string]string

Export returns all environment variables as a map.

func ExportWithPrefix

func ExportWithPrefix(prefix string) map[string]string

ExportWithPrefix returns environment variables matching a prefix.

func Load

func Load(cfg any) error

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

func LoadDotEnv(path string) error

LoadDotEnv loads environment variables from a .env file. It does NOT override existing environment variables.

func LoadDotEnvOverride

func LoadDotEnvOverride(path string) error

LoadDotEnvOverride loads environment variables from a .env file. It DOES override existing environment variables.

func LoadWithPrefix

func LoadWithPrefix(cfg any, prefix string) error

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 MustLoad

func MustLoad(cfg any)

MustLoad is like Load but panics on error.

func MustLoadDotEnv

func MustLoadDotEnv(path string)

MustLoadDotEnv loads a .env file or panics.

func MustLoadWithPrefix

func MustLoadWithPrefix(cfg any, prefix string)

MustLoadWithPrefix is like LoadWithPrefix but panics on error.

func RegisterParser

func RegisterParser[T any](parser func(string) (T, error))

RegisterParser registers a custom parser for a specific type T. This parser will be used when loading structs with fields of type T.

func Set

func Set(key, value string)

Set sets an environment variable.

func Unset

func Unset(key string)

Unset removes an environment variable.

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

type ParseError struct {
	Key   string
	Value string
	Err   error
}

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

type ValidationError struct {
	Field   string
	Message string
}

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

func New[T any](key string) *Var[T]

New creates a new environment variable of type T. It searches for a registered parser or uses encoding.TextUnmarshaler.

func (*Var[T]) Default

func (v *Var[T]) Default(value T) *Var[T]

Default sets the default value if the environment variable is not set.

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]) GetE

func (v *Var[T]) GetE() (T, error)

GetE returns the value of the environment variable or an error.

func (*Var[T]) IsSet

func (v *Var[T]) IsSet() bool

IsSet returns whether the environment variable is set.

func (*Var[T]) Lookup

func (v *Var[T]) Lookup() (T, bool)

Lookup returns the value and whether it was 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

func (v *Var[T]) Required() *Var[T]

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

func (v *Var[T]) WithPrefix(prefix string) *Var[T]

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
struct command
Example: Struct-based configuration loading
Example: Struct-based configuration loading

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL