env

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package env loads configuration from environment variables.

Env loads environment variables whose names starts with the given prefix and returns them as a nested map[string]any. Environment variables with empty values are treated as unset.

It splits the names by delimiter. For example, with the default delimiter "_", the environment variable `PARENT_CHILD_KEY="1"` is loaded as `{PARENT: {CHILD: {KEY: "1"}}}`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Env

type Env struct {
	// contains filtered or unexported fields
}

Env is a Provider that loads configuration from environment variables.

To create a new Env, call New.

func New

func New(opts ...Option) Env

New creates an Env with the given Option(s).

func (Env) Load

func (e Env) Load() (map[string]any, error)

func (Env) String

func (e Env) String() string

type Option

type Option func(*options)

Option configures an Env with specific options.

func WithNameSplitter added in v0.5.0

func WithNameSplitter(splitter func(string) []string) Option

WithNameSplitter provides the function used to split environment variable names into nested keys. If it returns an nil/[]string{}/[]string{""}, the variable will be ignored.

For example, with the default splitter, an environment variable name like "PARENT_CHILD_KEY" would be split into "PARENT", "CHILD", and "KEY".

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix provides the prefix used when loading environment variables. Only environment variables with names that start with the prefix will be loaded.

For example, if the prefix is "server", only environment variables whose names start with "server" will be loaded. By default, it has no prefix which loads all environment variables.

Jump to

Keyboard shortcuts

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