env

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 8 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
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

type Env map[string]string

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

func AsEnv(slice ...string) (Env, error)

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

func FromDotEnv(path string) (Env, error)

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

func (e *Env) Add(keyValues ...string) error

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

func (e *Env) AddPair(key, value string) error

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

func (e *Env) AsSlice() []string

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) Delete added in v0.0.13

func (e *Env) Delete(key string)

Delete removes an environment variable by key.

func (*Env) Exists added in v0.0.13

func (e *Env) Exists(key string) bool

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

func (e *Env) Export() error

Export applies the environment to the current process. Expands any variable references in the values.

func (*Env) Get

func (e *Env) Get(key string) string

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

func (e *Env) GetAny(keys ...string) string

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

func (e *Env) GetAsEnv(key string) string

GetAsEnv retrieves an environment variable's value as a formatted string.

func (*Env) GetOrDefault

func (e *Env) GetOrDefault(key, defaultValue string) string

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

func (e *Env) GetWithPredicates(predicates ...Predicate) Env

GetWithPredicates retrieves all environment values that match any of the provided predicates.

func (*Env) Keys added in v0.0.13

func (e *Env) Keys() []string

Keys returns the keys of the environment variables, sorted alphabetically.

func (*Env) Merge

func (e *Env) Merge(envs ...Env)

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

func (e *Env) MergedWith(envs ...Env) Env

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

func (e *Env) MergedWithX(env Env, envs ...Env) Env

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

func (e *Env) MustGet(key string) (string, error)

MustGet retrieves an environment variable's value by key. Returns an error if the key doesn't exist in the environment.

func (*Env) Normalized

func (e *Env) Normalized() Env

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.

type Predicate added in v0.0.13

type Predicate func(string, string) bool

Predicate defines a function type that takes two strings and returns a boolean indicating whether the predicate is satisfied.

Jump to

Keyboard shortcuts

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