Documentation
¶
Overview ¶
Package env provides utilities for managing environment variables in Go. It offers functionality for loading, manipulating, and normalizing environment variables from various sources, such as the system environment or .env files. Additionally, it allows environment variable expansion, filtering, and conversion between different formats.
Key features include: - Loading environment variables from the OS or .env files. - Adding, merging, and normalizing environment variables. - Expanding environment variable values based on the current system environment. - Converting environment variables to and from slices of `key=value` strings.
Index ¶
- Variables
- type Env
- func (e *Env) Add(keyValues ...string) error
- func (e *Env) AddPair(key, value string) error
- func (e *Env) AsSlice() []string
- func (e *Env) Delete(key string)
- func (e *Env) Exists(key string) bool
- func (e *Env) Expand()
- func (e *Env) Export() error
- func (e *Env) Get(key string) string
- func (e *Env) GetAny(keys ...string) string
- func (e *Env) GetAsEnv(key string) string
- func (e *Env) GetOrDefault(key, defaultValue string) string
- func (e *Env) GetWithPredicates(predicates ...Predicate) Env
- func (e *Env) Keys() []string
- func (e *Env) Merge(envs ...Env)
- func (e *Env) MergedWith(envs ...Env) Env
- func (e *Env) MergedWithX(env Env, envs ...Env) Env
- func (e *Env) MustGet(key string) (string, error)
- func (e *Env) Normalized() Env
- type Predicate
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEnvVarNotFound is returned when an expected environment variable is not found. ErrEnvVarNotFound = errors.New("environment variable not found") // ErrEnvMalformed is returned when an environment variable is malformed or contains invalid data. ErrEnvMalformed = errors.New("environment variable is malformed") )
Functions ¶
This section is empty.
Types ¶
type Env ¶
Env represents a map of environment variables, where the keys and values are strings. It can be used to access and manage environment settings.
func AsEnv ¶ added in v0.0.13
AsEnv creates an Env from a list of key-value strings. Each string should be in the format "key=value". Returns an error if any string is malformed. Normalizes the resulting environment.
func FromDotEnv ¶
FromDotEnv loads environment variables from a .env file. Reads and parses the file at the given path using godotenv. Returns the variables as a normalized Env map or an error if the file cannot be read or parsed.
func FromEnv ¶
func FromEnv() Env
FromEnv creates an Env from the current process environment. Retrieves all environment variables using os.Environ, normalizes their format, and returns them as an Env map.
func (*Env) Add ¶
Add parses and adds a key-value pairs to the environment. Takes strings in the format "key=value" and adds it to the Env map. Returns errors for strings that do not contain an '=' or have an empty key.
func (*Env) AddPair ¶ added in v0.0.13
AddPair adds a key-value pair to the environment. Takes a key and value string and adds it to the Env map. Returns an error if the key is empty.
func (*Env) AsSlice ¶ added in v0.0.13
AsSlice converts the environment to a string slice. Returns a sorted slice where each element is a "key=value" string representing an environment variable.
func (*Env) Exists ¶ added in v0.0.13
Exists checks if a key exists in the environment. Returns true if the key exists, false otherwise.
func (*Env) Expand ¶
func (e *Env) Expand()
Expand processes environment variable references in values. Replaces ${var} or $var patterns in each value with the corresponding environment variable value. Modifies the environment in place.
func (*Env) Export ¶ added in v0.0.13
Export applies the environment to the current process. Expands any variable references in the values.
func (*Env) Get ¶
Get retrieves an environment variable's value by key. Returns an empty string if the key doesn't exist.
func (*Env) GetAny ¶ added in v0.0.8
GetAny retrieves the first available environment value. Tries each key in order until a value is found, returning an empty string if none of the keys exist.
func (*Env) GetAsEnv ¶ added in v0.0.13
GetAsEnv retrieves an environment variable's value as a formatted string.
func (*Env) GetOrDefault ¶
GetOrDefault retrieves an environment value with fallback. Returns the environment value if the key exists, otherwise returns the provided default value.
func (*Env) GetWithPredicates ¶ added in v0.0.13
GetWithPredicates retrieves all environment values that match any of the provided predicates.
func (*Env) Keys ¶ added in v0.0.13
Keys returns the keys of the environment variables, sorted alphabetically.
func (*Env) Merge ¶
Merge combines multiple environments into the current one. Copies values from the provided environments, preserving existing values in case of key conflicts.
func (*Env) MergedWith ¶ added in v0.0.13
MergedWith creates a new environment by combining multiple environments. Returns a new Env containing all values from this environment plus any non-conflicting values from the provided environments. Does not modify the original environment.
func (*Env) MergedWithX ¶ added in v0.1.0
MergedWithX creates a new environment by combining multiple environments. Returns a new Env containing all values from this environment plus any non-conflicting values from the provided environments. Does not modify the original environment.
func (*Env) MustGet ¶ added in v0.0.8
MustGet retrieves an environment variable's value by key. Returns an error if the key doesn't exist in the environment.
func (*Env) Normalized ¶
Normalized returns a platform-appropriate normalized environment. On Windows, converts all keys to uppercase for case-insensitive compatibility. On other systems, returns the environment unchanged.