Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Expand ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.