denv

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 7 Imported by: 0

README

denv

denv ("dee-env") is a dependency-free Go package for parsing .env ("dot-env") files and manipulating their contents. Originally developed for the job scheduler Regular, it is suitable for general use in Go projects.

go get dbohdan.com/denv

denv is mostly compatible with GoDotEnv. It is tested against test fixtures imported from GoDotEnv. The one major difference is that attempting to substitute a nonexistent variable results in an error. However, .env files have no formal specification, and differences in parsing around edge cases are to be expected.

Features

  • Parse .env files with quoted multiline values, backslash escape sequences, and comments
  • Perform optional shell-style variable substitution
  • Control substitution through quoting in the file: use double quotes or no quotes to enable it, single quotes to disable it
  • Merge environments
  • Convert between []string{"FOO=bar", ...} and an environment map type

Examples

Parse a .env file with variable substitution:

content := strings.NewReader(`
# Set the base directory.
BASE=/opt
# Use substitution.
PATH=${BASE}/bin
`)

env, err := denv.Parse(content, true, nil)
// env = map[string]string{"BASE": "/opt", "PATH": "/opt/bin"}

Load from a file:

// Use the contents of os.Environ for substitution.
substEnv := denv.OS()
env, err := denv.Load(".env", true, substEnv)

Convert environment strings:

// From string slice to map.
env := denv.EnvFromStrings([]string{"FOO=bar", "BAZ=qux"})

// Back to a string slice.
strings := env.Strings()

Merge multiple environments:

merged := denv.Merge(env1, env2, env3)

License

MIT. See the file LICENSE.

Documentation

Overview

Package denv parses .env files and manipulates their contents. It supports shell-style variable substitution, quoted values, comments, environment merging, and conversion between string slices and its own environment map type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Env

type Env map[string]string

Env represents a mapping of environment variable names to their values.

func EnvFromStrings

func EnvFromStrings(strs []string) Env

EnvFromStrings creates an Env from a slice of "KEY=VALUE" strings.

func Load

func Load(filePath string, subst bool, substEnv Env) (Env, error)

Load reads and parses an environment file at the given path. If subst is true, it performs variable substitution using values from the same file and substEnv.

func Merge

func Merge(envs ...Env) Env

Merge combines multiple environments into a single Env. Later environments override values from earlier ones.

func OS

func OS() Env

OS returns the current process environment as an Env.

func Parse

func Parse(r io.Reader, subst bool, substEnv Env) (Env, error)

Parse reads environment variables from an io.Reader and returns them as a map. If subst is true, it substitutes variables from the same env file and substEnv.

func (Env) Keys

func (e Env) Keys() []string

Keys returns a sorted slice of environment variable names.

func (Env) Strings

func (e Env) Strings() []string

Strings converts the environment map to a slice of "KEY=VALUE" strings.

Jump to

Keyboard shortcuts

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