Documentation
¶
Overview ¶
Package dotenv loads environment variables from .env files.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load loads environment variables from .env files into the current process environment.
12-Factor Preservation: Existing OS environment variables are never overwritten.
Hierarchy: When paths is empty, Load automatically loads ".env" followed by ".env.local" from the current working directory if they exist (silently ignoring missing files). When explicit paths are provided, each file must exist or a DotenvError is returned.
Precedence: Files are applied in order: a key defined in more than one file takes the value from the last file that defines it, while keys already present in the process environment outrank every file.
Variable references resolve from the process environment first, then from keys defined earlier in the same file; a file that overrides a key already supplied by an earlier file in the same call expands to its own value.
func LoadWith ¶ added in v0.2.0
LoadWith loads environment variables from .env files using setenv to set each variable. Passing the method value of testing.T.Setenv in tests ensures the variables loaded from .env files are restored automatically when the test finishes. When setenv is nil, LoadWith behaves like Load and sets the variables through os.Setenv.
Resolution follows the same hierarchy, precedence, variable expansion, and 12-factor preservation rules as Load; the process environment remains the host view, so a setenv that does not mutate it (a collecting callback) also does not participate in later files' expansion.
func LoadWithOrigins ¶ added in v0.3.0
func LoadWithOrigins(setenv func(key, value string), origins map[string]string, mustExist bool, paths ...string) error
LoadWithOrigins loads environment variables from .env files using setenv to set each variable, recording the source file of every variable it sets in origins (environment key to file path). A non-nil origins map may be shared across calls: a key already recorded there was set by an earlier .env file in the same run, so a later file is allowed to override it, while any other key present in the process environment is preserved. When setenv is nil, variables are set through os.Setenv.
When paths is empty, LoadWithOrigins probes ".env" and ".env.local" in the current working directory, silently ignoring missing files. When paths are given, each is loaded with the caller-supplied mustExist policy: a missing file is ignored when mustExist is false and fails with a DotenvError otherwise. Malformed or oversized files always fail. Resolution follows the same precedence, variable expansion, and 12-factor preservation rules as Load.
Types ¶
type DotenvError ¶
DotenvError represents an error reading or parsing a .env file.
func (*DotenvError) Error ¶
func (e *DotenvError) Error() string
func (*DotenvError) Is ¶
func (e *DotenvError) Is(target error) bool
func (*DotenvError) Unwrap ¶
func (e *DotenvError) Unwrap() error