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(kv string) error
- func (e *Env) Expand()
- func (e Env) Get(key string) string
- func (e Env) GetAll(predicate func(key, value string) bool) Env
- func (e Env) GetAllMatching(pattern string) (Env, error)
- func (e Env) GetAllWithPrefix(prefix string) Env
- func (e Env) GetAllWithSuffix(suffix string) Env
- func (e Env) GetAllWithValue(value string) Env
- func (e Env) GetAny(keys ...string) string
- func (e Env) GetOrDefault(key, defaultValue string) string
- func (e Env) Has(key string) bool
- func (e *Env) Merge(envs ...Env)
- func (e Env) Merged(envs ...Env) Env
- func (e Env) MustGet(key string) (string, error)
- func (e Env) Normalize() Env
- func (e Env) Normalized() Env
- func (e Env) ToEnv() error
- func (e Env) ToSlice() []string
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 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 FromSlice ¶
FromSlice 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 (*Env) Add ¶
Add parses and adds a key-value pair to the environment. Takes a string in the format "key=value" and adds it to the Env map. Returns an error if the string doesn't contain exactly one '=' separator.
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) Get ¶
Get retrieves an environment variable's value by key. Returns an empty string if the key doesn't exist.
func (Env) GetAll ¶
GetAll filters environment variables using a predicate. Returns a new Env containing only the key-value pairs for which the predicate function returns true.
func (Env) GetAllMatching ¶
GetAllMatching returns environment variables by regex pattern. Returns a new Env containing only variables whose keys match the specified regex pattern. Returns an error if the pattern is invalid.
func (Env) GetAllWithPrefix ¶
GetAllWithPrefix returns environment variables by prefix. Returns a new Env containing only variables whose keys start with the specified prefix.
func (Env) GetAllWithSuffix ¶
GetAllWithSuffix returns environment variables by suffix. Returns a new Env containing only variables whose keys end with the specified suffix.
func (Env) GetAllWithValue ¶
GetAllWithValue returns environment variables by value. Returns a new Env containing only variables that have the exact specified value.
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) GetOrDefault ¶
GetOrDefault retrieves an environment value with fallback. Returns the environment value if the key exists, otherwise returns the provided default value.
func (Env) Has ¶ added in v0.0.11
Has checks if a key exists in the environment. Returns true if the key exists, false otherwise.
func (*Env) Merge ¶
Merge combines multiple environments into the current one. Copies values from the provided environments into this one, preserving existing values in case of key conflicts.
func (Env) Merged ¶
Merged 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) Normalize ¶
Normalize creates a new environment with uppercase keys. Returns a copy of the environment with all keys converted to uppercase, which ensures consistent behavior on case-insensitive systems like Windows.
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.