dotenv

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 10 Imported by: 0

README

dotenv

Tests Status Go Report Card Go Reference

The most stupid simple dotenv package.

Multiline is not allowed, dotenv considers the key being what's before the first equal sign, and the value being what's after up to the line break. No quotes or escapes needed.

Install

go get github.com/rfberaldo/dotenv

Usage

Load accepts multiple filepaths, if none is given, ".env" is used.

if err := dotenv.Load(); err != nil {
  log.Fatal(err)
}

Variables are expanded by default, meaning that if your file has:

PORT=8000
HOST=localhost:${PORT}
URL=http://${HOST}

After loading, os.Getenv("URL") would produce http://localhost:8000.

Overriding

By default, dotenv won't overwrite existing environment variables, the first file to set a variable will have priority.

To make it override, use SetOverride. In this mode, the last file to set a variable will have priority.

dotenv.SetOverride(true)
if err := dotenv.Load(".env", ".env.production"); err != nil {
  log.Fatal(err)
}

By default, dotenv won't return error if file do not exists. To force a file to exist, use SetRequireFileExists.

dotenv.SetRequireFileExists(true)
if err := dotenv.Load(); err != nil {
  log.Fatal(err)
}
Reading

It's also possible to get a key-value pair map instead of populating the environment.

Read follows the same rules as Load, but will return a map. Predefined vars are only considered for expanding.

kv, err := dotenv.Read() // returns map[string]string
if err != nil {
  log.Fatal(err)
}
Testing

dotenv exposes a helper function for testing called LoadTesting. It will use t.Setenv() instead of os.Setenv.

if err := dotenv.LoadTesting(t); err != nil {
  t.Fatal(err)
}
Parsing

Parse is a primitive, it receives a string and returns a key-value pair map. However will not expand the variables.

str := "PORT=8000\nHOST=localhost:${PORT}"
kv, err := dotenv.Parse(strings.NewReader(str))
// Output: map[string]string{
//   "PORT": "8000",
//   "HOST": "localhost:${PORT}",
// }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(paths ...string) error

Load loads env vars from multiple sources and sets them using os.Setenv.

Load will not overwrite vars by default, first to set wins. To make it override use SetOverride, when overriding, last to set wins.

Load will ignore if file do not exists by default, use SetRequireFileExists to force a file to exist.

Paths should be forward-slash separated, if no path is given ".env" will be used.

func LoadTesting added in v1.1.0

func LoadTesting(t testing.TB, paths ...string) error

LoadTesting is like Load, but sets vars using [t.Setenv], useful for tests.

func Parse

func Parse(r io.Reader) (map[string]string, error)

Parse reads every line from r, empty lines and lines that starts with '#' are discarded.

Value should not contain line breaks or quotes, all characters after the first equal sign up to the line break are considered. Values are not expanded.

func Read added in v1.1.0

func Read(paths ...string) (map[string]string, error)

Read is like Load, but returns a map instead of setting the environment. Predefined vars are only considered for expanding.

func SetOverride

func SetOverride(value bool)

SetOverride makes Load and Read override previous env vars.

func SetRequireFileExists

func SetRequireFileExists(value bool)

SetRequireFileExists makes Load and Read return an error if any of the given files do not exist.

Types

This section is empty.

Jump to

Keyboard shortcuts

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