env

package
v1.1.14 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(s string, opts ...Option) (string, error)

Expand substitutes environment variables in a string.

It replaces references to environment variables in the formats ${KEY} or $KEY with their corresponding values. A literal dollar sign can be escaped with $$. If a referenced variable is not found in the environment, the function returns an error. Its behavior can be adjusted through functional options.

func Unmarshal

func Unmarshal(v any, opts ...Option) error

Unmarshal populates the fields of a struct with values from environment variables. The given value v must be a non-nil pointer to a struct.

By default, Unmarshal processes all exported fields. A field's environment variable name is derived from its name, converted to uppercase SNAKE_CASE. To ignore a field, tag it with `env:"-"`. Unexported fields are always excluded. If a variable is not set, the field remains unchanged unless a default value is specified in the struct tag, or it is marked as required.

Types

type Lookup

type Lookup func(key string) (string, bool)

Lookup is a function that retrieves the value of an environment variable. It follows the signature of os.LookupEnv, returning the value and a boolean indicating whether the variable was present. This type allows for custom lookup mechanisms, such as reading from sources other than the actual environment, which is especially useful for testing.

type Option

type Option func(*config)

Option is a function that configures the behavior of the Unmarshal and Expand functions. It follows the functional options pattern.

func WithLookup

func WithLookup(lookup Lookup) Option

WithLookup returns an Option that sets a custom lookup function for retrieving environment variable values. If not customized, os.LookupEnv will be used by default. This is useful for testing or if you need to load environment variables from alternative sources.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix returns an Option that adds a common prefix to all environment variable keys looked up during unmarshaling. For example, WithPrefix("APP_") would cause a field with the env tag "PORT" to look for the "APP_PORT" variable.

type Unmarshaler

type Unmarshaler interface {
	// UnmarshalEnv unmarshals the string value of an environment variable.
	// The value is the raw string from the environment or a default value.
	// It returns an error if the value cannot be parsed.
	UnmarshalEnv(value string) error
}

Unmarshaler is an interface that can be implemented by types to provide their own custom logic for parsing an environment variable string.

Jump to

Keyboard shortcuts

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