dotenvx

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: BSD-3-Clause Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dotenvx-go

A pure-Go port of the dotenvx loading library. It reads .env files, including encrypted ones, straight into your Go process, with no Node and no dotenvx binary.

import "github.com/narasaka/dotenvx-go"

func main() {
	if err := dotenvx.Load(); err != nil {
		log.Fatal(err)
	}
	// os.Getenv("HELLO") now works, decrypted if it was encrypted.
}

It ports the parsing and decryption of require('@dotenvx/dotenvx').config(), not dotenvx run. It is a community port, as godotenv is of dotenv. Use the dotenvx CLI to encrypt, set and edit values; this library only loads them. See what it doesn't do.

In the benchmark, with nothing else running dotenvx, a server loading its file in process used 73 to 89 MiB less than under dotenvx run, which keeps the CLI running beside it. Of that, 41 to 57 MiB is memory a second dotenvx run couldn't share. It started 0.4 s (20 values) to 2.9 s (200 values) sooner than under dotenvx run or dotenvx get.

Install

go get github.com/narasaka/dotenvx-go

It needs Go 1.24 or later. Its one dependency is decred's secp256k1, because the standard library doesn't have that curve. Unlike the library dotenvx uses, it isn't written to resist timing attacks, so a process sharing the machine could learn a private key by watching decryption; see Decrypt.

Loading

dotenvx.Load()                        // .env, keeping variables already set
dotenvx.Load(".env.local", ".env")    // the first file to set a key wins
dotenvx.Overload(".env", ".env.local") // replace variables; the last file wins
values, err := dotenvx.Read(".env")   // the values, without touching the environment

With no files, Load reads the files named in DOTENV_FILE, or else DOTENV_PATH, or else DOTENV_F, or the ones the DOTENV_PRIVATE_KEY* variables imply, or else .env, as dotenvx does. A missing file is an error that wraps fs.ErrNotExist, so .env can be optional:

if err := dotenvx.Load(".env"); err != nil && !errors.Is(err, fs.ErrNotExist) {
	log.Fatal(err)
}

Values are parsed exactly as dotenvx parses them. That covers comments, export, single, double and backtick quotes, multiline values, and UTF-16LE files with a byte order mark. It also expands $VAR, ${VAR}, ${VAR:-default} and the other forms, and substitutes $(command) output. The port passes all 88 scenarios of dotenvx's conformance suite.

dotenvx.Options holds the settings: the environment to resolve against, overload, key files, a key provider, and a switch to turn off command substitution.

Encrypted files

Encrypt a .env with the dotenvx CLI:

dotenvx encrypt

That replaces each value with encrypted:…, except for keys a Dotenvxfile stores as plaintext and, unless it says otherwise, keys ending in _PLAIN. The first time, it asks where to keep the private key (in CI or without a terminal it uses .env.keys without asking). Choose the .env.keys file and keep it out of version control: this library doesn't read key stores, so a key kept in one has to be exported and set as DOTENV_PRIVATE_KEY, or fetched through Options.KeyProvider. Enclaved dotenvx Armor keys never leave their remote enclave, so this library can't use them.

Load finds .env.keys next to each file, except that, as in dotenvx, it reads .env.keys in the current directory when the file's own key variable is already set (DOTENV_PRIVATE_KEY_PRODUCTION for .env.production). Options.KeyFiles names the key files explicitly. In production, set only the private key:

DOTENV_PRIVATE_KEY=… ./your-app

Private keys come from:

  • .env.keys files. As in dotenvx, a file that declares its public key (DOTENV_PUBLIC_KEY) only takes the keys of that public key, and of any DOTENV_PUBLIC_KEY* variables in the environment, from .env.keys. A file that declares none takes every key in it.
  • DOTENV_PRIVATE_KEY* variables, each holding one key. Their names follow the files by convention, but each one is tried for every file.
  • Options.KeyProvider, a hook for key stores. If it fails, the file is read with every key in .env.keys, and its error is returned too.

A value that can't be decrypted keeps its encrypted:… text, and Load reports it in an error that matches dotenvx.ErrDecryptionFailed, while still loading everything else.

Embedding

Once every secret in it is encrypted, the file is safe to compile into the binary, so a single binary can carry its configuration and only needs the key at run time:

package main

import (
	"bytes"
	_ "embed"
	"log"
	"os"

	"github.com/narasaka/dotenvx-go"
)

//go:embed .env
var envFile []byte

func main() {
	values, err := dotenvx.Parse(bytes.NewReader(envFile))
	if err != nil {
		log.Fatal(err)
	}
	for k, v := range values {
		if err := os.Setenv(k, v); err != nil {
			log.Fatalf("setting %s: %v", k, err)
		}
	}
}

With DOTENV_PRIVATE_KEY set, Parse decrypts the values; existing variables keep their values, as with Load.

Encrypt, Decrypt, PublicKey and GenerateKeypair are there too, for tools and tests.

What it doesn't do

dotenvx's own config() does more than load values. It checks the loaded values against Dotenvxfile policies (required keys, types, allowed values and ranges, failing hard under strict), redacts secrets from the program's output as those policies say, by patching Node's output streams, and it can get keys from key stores (1Password, Bitwarden, the system keychain, dotenvx Armor). This port does none of that, and doesn't print dotenvx's inject summary either.

A Go library can't redact output reliably anyway: writes straight to file descriptors 1 and 2, cgo and child processes would get past it. That's what dotenvx run is for. It starts your program as a child process, so it sees all of its output, and it is where dotenvx's upcoming credential brokering will live too. Encrypting and editing files also stay with the CLI. Options.KeyProvider is the place to plug in a key store.

Command substitution

As in dotenvx, KEY=$(command) runs the command with /bin/sh while loading, and so does an existing value of a key the file sets (single-quoted values such as KEY='$(command)' are never run). That's surprising in a library, and on Windows or in minimal container images, where there is no /bin/sh, the value is kept as it is. It can be turned off:

dotenvx.Options{DisableCommandSubstitution: true}.Load()

Compatibility

The port follows Node's @dotenvx/primitives 4.3.0, which is what dotenvx runs, including where the Rust implementation differs. It deviates where dotenvx relies on accidents of JavaScript (how String.prototype.replace treats $$ in a value, for example), where dotenvx would hang or crash, and in a few places where Go calls for it: commands always run with /bin/sh, even on Windows, where dotenvx uses cmd.exe, paths are used as given, without ~ expansion, and keys must be exactly 64 hex digits. Each deviation is documented in the godoc where it applies.

License

BSD 3-Clause, like dotenvx, whose logic this ports. See LICENSE.

Documentation

Overview

Package dotenvx loads dotenvx-compatible .env files, including encrypted ones, into a Go process.

It ports the parsing and decryption of dotenvx's config() (https://dotenvx.com), reading files in process, with no Node and no dotenvx binary. It doesn't redact output, use key stores or apply Dotenvxfile policies, as config() can; for redaction, run the program with dotenvx run. Encrypting and editing files stay with the dotenvx CLI.

Load reads .env into the process environment, keeping variables that are already set; Overload replaces them; Read and Parse return the values without touching the environment. Options configures all four.

Values are parsed as dotenvx parses them, including quotes, multiline values, $VAR and ${VAR:-default} expansion and $(command) substitution. Values that start with "encrypted:" are decrypted with private keys from .env.keys files, DOTENV_PRIVATE_KEY* variables or Options.KeyProvider.

The port follows Node's @dotenvx/primitives, which is what dotenvx runs. Where it deliberately differs, mostly because dotenvx relies on an accident of JavaScript, the function concerned documents it.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingPrivateKey = &Error{Code: "MISSING_PRIVATE_KEY", Message: "could not decrypt because private key is missing"}
	ErrInvalidPrivateKey = &Error{Code: "INVALID_PRIVATE_KEY", Message: "could not decrypt using private key"}
	ErrWrongPrivateKey   = &Error{Code: "WRONG_PRIVATE_KEY", Message: "could not decrypt using private key"}
	ErrDecryptionFailed  = &Error{Code: "DECRYPTION_FAILED", Message: "could not decrypt value"}
	ErrInvalidPublicKey  = &Error{Code: "INVALID_PUBLIC_KEY", Message: "could not encrypt using public key"}
	ErrEncryptionFailed  = &Error{Code: "ENCRYPTION_FAILED", Message: "could not encrypt value"}
)

These are for matching with errors.Is. The errors returned are new values with the same codes, so changing them doesn't change these. Parse, Read and Load report values they couldn't decrypt with an error that matches ErrDecryptionFailed and names their keys.

View Source
var (
	// ErrReferenceCycle is reported, wrapped, when a value can't be
	// expanded because a variable refers back to itself, as in A=$B with
	// B=x$B.
	ErrReferenceCycle = errors.New("reference cycle")

	// ErrExpansionLimit is reported, wrapped, when expanding a value would
	// take more than 10,000 substitutions of values that contain "$", or
	// make it longer than 16 MiB, as happens when each of a chain of
	// variables refers to the next one twice.
	ErrExpansionLimit = errors.New("expansion limit exceeded")
)

Functions

func Decrypt

func Decrypt(privateKey, value string) (string, error)

Decrypt decrypts a value encrypted for dotenvx with privateKey, a secp256k1 private key in hex. Like dotenvx, it returns values that don't start with "encrypted:" as they are.

Its errors match ErrMissingPrivateKey, ErrInvalidPrivateKey, ErrWrongPrivateKey (when the value was encrypted for another key) and ErrDecryptionFailed (when it isn't a valid encrypted value), as dotenvx's do. Unlike dotenvx, it parses the private key strictly as 64 hex digits: Node reads hex up to the first character that isn't. It skips characters outside ASCII in the base64, which Node reads by the low byte of their UTF-16 code units. And it returns plaintext that isn't valid UTF-8 as it is, where dotenvx gives U+FFFD.

The library dotenvx uses is written to make timing attacks harder; Decrypt isn't. The steps it takes depend on the private key, so another process that can watch it run on the same machine, by timing it or through the CPU's caches, could learn the key. That doesn't matter where nothing untrusted shares the machine. PublicKey and GenerateKeypair have the same weakness, and so does Encrypt, which could give away the value it encrypts. Parse, Read, Load and Overload decrypt with Decrypt.

Example
package main

import (
	"errors"
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	// The private key and an encrypted value from testdata/encrypted.
	const privateKey = "ec9e80073d7ace817d35acb8b7293cbf8e5981b4d2f5708ee5be405122993cd1"
	value, err := dotenvx.Decrypt(privateKey, "encrypted:BG8M6U+GKJGwpGA42ml2erb9+T2NBX6Z2JkBLynDy21poz0UfF5aPxCgRbIyhnQFdWKd0C9GZ7lM5PeL86xghoMcWvvPpkyQ0yaD2pZ64RzoxFGB1lTZYlEgQOxTDJnWxODHfuQcFY10uA==")
	fmt.Println(value, err)

	other, _ := dotenvx.GenerateKeypair()
	_, err = dotenvx.Decrypt(other, "encrypted:BG8M6U+GKJGwpGA42ml2erb9+T2NBX6Z2JkBLynDy21poz0UfF5aPxCgRbIyhnQFdWKd0C9GZ7lM5PeL86xghoMcWvvPpkyQ0yaD2pZ64RzoxFGB1lTZYlEgQOxTDJnWxODHfuQcFY10uA==")
	fmt.Println(errors.Is(err, dotenvx.ErrWrongPrivateKey), err)
}
Output:
encrypted <nil>
true [WRONG_PRIVATE_KEY] could not decrypt using private key

func Encrypt

func Encrypt(publicKey, value string) (string, error)

Encrypt encrypts value for publicKey, a secp256k1 public key in hex, compressed or not and optionally starting with "0x", the way dotenvx does. Encryption is randomized, so encrypting the same value twice gives different results. Its errors match ErrInvalidPublicKey, or ErrEncryptionFailed when the scheme isn't allowed, as with GODEBUG=fips140=only.

Example
package main

import (
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	privateKey, publicKey := dotenvx.GenerateKeypair()
	encrypted, err := dotenvx.Encrypt(publicKey, "World")
	if err != nil {
		fmt.Println(err)
		return
	}
	value, _ := dotenvx.Decrypt(privateKey, encrypted)
	fmt.Println(value)
}
Output:
World

func GenerateKeypair

func GenerateKeypair() (privateKey, publicKey string)

GenerateKeypair returns a new secp256k1 private key and its compressed public key, both in hex.

Example
package main

import (
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	privateKey, publicKey := dotenvx.GenerateKeypair()
	// 32 bytes, and a compressed point of 33, in hex.
	fmt.Println(len(privateKey), len(publicKey))
}
Output:
64 66

func Load

func Load(files ...string) error

Load loads env files into the process environment with the default Options. Variables that are already set keep their values.

Example
package main

import (
	"errors"
	"fmt"
	"io/fs"
	"log"
	"os"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	// Load .env from the current directory, if there is one, keeping
	// variables that are already set.
	if err := dotenvx.Load(".env"); err != nil && !errors.Is(err, fs.ErrNotExist) {
		log.Fatal(err)
	}
	fmt.Println(os.Getenv("HELLO"))
}

func Overload

func Overload(files ...string) error

Overload loads env files into the process environment, replacing variables that are already set.

Example
package main

import (
	"log"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	// Overload replaces variables that are already set, and .env.local,
	// read last, wins over .env.
	if err := dotenvx.Overload(".env", ".env.local"); err != nil {
		log.Fatal(err)
	}
}

func Parse

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

Parse reads dotenv source from r with the default Options.

func PublicKey

func PublicKey(privateKey string) (string, error)

PublicKey returns the compressed public key, in hex, of privateKey, a secp256k1 private key in hex. dotenvx files name the keys they are encrypted for by this public key.

Example
package main

import (
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	publicKey, err := dotenvx.PublicKey("ec9e80073d7ace817d35acb8b7293cbf8e5981b4d2f5708ee5be405122993cd1")
	fmt.Println(publicKey, err)
}
Output:
03eaf2142ab3d55bdf108962334e06696db798e7412cfc51d75e74b4f87f299bba <nil>

func Read

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

Read reads env files with the default Options.

Types

type Error

type Error struct {
	Code    string
	Message string
	// Err is the underlying error, if any.
	Err error
}

Error is an error with one of dotenvx's codes, such as "WRONG_PRIVATE_KEY". Its message has dotenvx's "[CODE] message" form, followed by the underlying error, if any. errors.Is matches an Error against the Err variables by code alone.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is reports whether target is an *Error with the same code.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error.

type Options

type Options struct {
	// Env is the existing environment that Parse and Read resolve against,
	// in the "KEY=value" form of os.Environ. A key that already exists in
	// Env keeps its existing value unless Overload is set. If Env is nil,
	// they use the current process environment; use an empty slice for no
	// existing environment at all. Load ignores Env: it always resolves
	// against the process environment it sets values in.
	Env []string

	// Overload lets values from the source replace values that already
	// exist in Env.
	Overload bool

	// DisableCommandSubstitution turns off $(command) substitution. By
	// default, as in dotenvx, each $(command) in a value that isn't
	// single-quoted is replaced with the output of running the command with
	// /bin/sh. That includes the existing value of a key the source
	// assigns, unless Overload is set. The command sees Env plus the values
	// parsed before it. When a command fails, writes more than 1 MiB to
	// stdout and stderr together, or can't run because there is no /bin/sh
	// (as on Windows or in minimal container images), the value is kept as
	// it is. What a command writes to stderr is written to os.Stderr after
	// it finishes, as dotenvx does.
	DisableCommandSubstitution bool

	// KeyFiles are the files, usually named .env.keys, to read the private
	// keys of encrypted values from: the values of their DOTENV_PRIVATE_KEY*
	// keys, each of which may list several keys separated by commas. Files
	// that can't be read are skipped. If KeyFiles is nil, Parse reads
	// .env.keys in the current directory, and Read and Load the .env.keys
	// next to each env file, as dotenvx does; but when that file's own
	// private key is set in the environment (DOTENV_PRIVATE_KEY_PRODUCTION
	// for .env.production), dotenvx reads .env.keys in the current
	// directory instead, and so do they. Use an empty slice to read none.
	//
	// Like dotenvx's config, a source that declares its public keys, in
	// DOTENV_PUBLIC_KEY* values, only takes the private keys of those (and
	// of the environment's DOTENV_PUBLIC_KEY* variables) from the key
	// files, so a value encrypted for another key in .env.keys stays
	// encrypted. A source that declares none takes every key.
	//
	// Private keys also come from the DOTENV_PRIVATE_KEY* variables of the
	// existing environment, each holding a single key, and always count.
	// Keys must be exactly 64 hex digits: a value with anything else in it,
	// such as a comma-separated list or trailing whitespace, is ignored,
	// where dotenvx would use the hex it starts with.
	KeyFiles []string

	// KeyProvider, if set, is asked for the private key of each public key
	// that has none from KeyFiles or the environment: those the source
	// declares in its DOTENV_PUBLIC_KEY* values, and those of the
	// environment's DOTENV_PUBLIC_KEY* variables. It returns the private key
	// in hex, or "" if it has none; an invalid key is ignored. It is where
	// key stores plug in. If it returns an error, the source is parsed
	// without it, with every key in the key files, as dotenvx does, and
	// the error is returned with the values.
	KeyProvider func(publicKey string) (privateKey string, err error)
}

Options configures how dotenv source is resolved. The zero value is ready to use and matches the defaults of dotenvx.

Example (KeyProvider)
package main

import (
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	// A provider looks private keys up by public key, for example in a key
	// store. This one knows the key of testdata/encrypted.
	keys := map[string]string{
		"03eaf2142ab3d55bdf108962334e06696db798e7412cfc51d75e74b4f87f299bba": "ec9e80073d7ace817d35acb8b7293cbf8e5981b4d2f5708ee5be405122993cd1",
	}
	opts := dotenvx.Options{
		Env:      []string{},
		KeyFiles: []string{}, // Don't read .env.keys.
		KeyProvider: func(publicKey string) (string, error) {
			return keys[publicKey], nil
		},
	}
	values, err := opts.Read("testdata/encrypted/.env")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(values["HELLO"])
}
Output:
encrypted

func (Options) Load

func (o Options) Load(files ...string) error

Load reads env files as Read does, and sets their values in the process environment, in the order dotenvx would set them. It ignores Env. Like Read, it sets every value it could read even if some files fail.

func (Options) Parse

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

Parse reads dotenv source from r and returns its values. It doesn't modify the process environment. When a key is assigned more than once, the last assignment wins.

Values that start with "encrypted:" are decrypted first, with the keys that KeyFiles, the environment and KeyProvider supply. A value that can't be decrypted stays encrypted, and isn't evaluated or expanded; Parse returns the values together with an error that matches ErrDecryptionFailed and names the keys it couldn't decrypt. Decryption isn't constant-time; see Decrypt.

Values that aren't single-quoted then have each $(command) replaced with the command's output, unless DisableCommandSubstitution is set. Each output is inserted as it is, where the command was. dotenvx instead treats it as a String.prototype.replace pattern ($$, $&, $` and $') and replaces the first copy of the command's text, which may be in an earlier command's output; it also turns invalid UTF-8 into U+FFFD, and runs commands with cmd.exe on Windows rather than /bin/sh.

Values that aren't single-quoted, and had no command substituted, then have their $NAME and ${NAME} references expanded, as do ${NAME:-default}, ${NAME-default}, ${NAME:+alternative} and ${NAME+alternative}, the way dotenvx expands them. Names resolve to existing values first, then to values parsed earlier, or the other way round with Overload. A backslash keeps a "$" from starting a reference. Existing values that aren't empty aren't expanded, though they do go through command substitution.

Expansion differs from dotenvx where dotenvx relies on accidents of JavaScript: a value is inserted as it is, even if it contains "$$" or "$&"; the reference that matched is the one replaced, even if an escaped copy of it comes first; and names containing "null", or naming properties of Object.prototype, resolve like any other. Where dotenvx would loop forever, because a variable refers back to itself, Parse leaves the rest of that value unexpanded and returns its values with an error that wraps ErrReferenceCycle. It does that as soon as a reference that came entirely from a variable's value would insert that value again, which also stops a few contrived inputs that dotenvx finishes. A value that would take more than 10,000 substitutions of values containing "$", or grow past 16 MiB, stops the same way, with ErrExpansionLimit.

Source that starts with a UTF-16LE byte order mark is decoded from UTF-16LE, with unpaired surrogates, which dotenvx keeps, becoming U+FFFD; anything else is read as UTF-8. Bytes that aren't valid UTF-8 are kept as they are, where dotenvx, which only sees decoded text, would have replaced them with U+FFFD. Keys such as constructor or __proto__, which make dotenvx throw, are parsed like any other key.

Example
package main

import (
	"fmt"
	"strings"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	src := `# comments and blank lines are skipped
HELLO=World
GREETING="Hello, ${HELLO}
on two lines"
EXISTING=from the file
`
	opts := dotenvx.Options{Env: []string{"EXISTING=from the environment"}}
	values, err := opts.Parse(strings.NewReader(src))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(values["HELLO"])
	fmt.Println(values["GREETING"])
	fmt.Println(values["EXISTING"])

	opts.Overload = true
	values, _ = opts.Parse(strings.NewReader(src))
	fmt.Println(values["EXISTING"])
}
Output:
World
Hello, World
on two lines
from the environment
from the file
Example (Embedded)

An encrypted .env can be compiled into the binary, which then only needs the private key at run time.

package main

import (
	"bytes"
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

//go:embed testdata/encrypted/.env
var encryptedEnv []byte

func main() {
	// In production, the private key would be the only dotenvx variable in
	// the environment: here, it is the only entry in Env, and there are no
	// key files.
	opts := dotenvx.Options{
		Env:      []string{"DOTENV_PRIVATE_KEY=ec9e80073d7ace817d35acb8b7293cbf8e5981b4d2f5708ee5be405122993cd1"},
		KeyFiles: []string{},
	}
	values, err := opts.Parse(bytes.NewReader(encryptedEnv))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(values["HELLO"])
}
Output:
encrypted

func (Options) Read

func (o Options) Read(files ...string) (map[string]string, error)

Read reads the given env files and returns their values without modifying the process environment. A file that is a directory stands for the .env in it. Paths are used as given: unlike the path option of dotenvx's config, a leading "~" isn't expanded and commas don't separate paths. With no files, like dotenvx, Read reads the comma-separated files named by DOTENV_FILE, DOTENV_PATH or DOTENV_F, the first that isn't empty; or else, if DOTENV_PRIVATE_KEY* variables are set, the files they stand for, in the environment's order (.env.production for DOTENV_PRIVATE_KEY_PRODUCTION); or else ".env". It looks for those in the existing environment only: with Env set, that is Env, where dotenvx would also check the process environment.

Files are read in order, and each one sees the values set by the files before it as existing values. So unless Overload is set, the first file to set a key wins, and with Overload the last one does.

A file that can't be read doesn't stop the others: Read returns the values it could read together with the errors. To make a single file optional, ignore an error that wraps fs.ErrNotExist. With several files, that would also ignore errors joined with it.

Example
package main

import (
	"fmt"

	_ "embed"
	"github.com/narasaka/dotenvx-go"
)

func main() {
	// Files are read in order, and the first to set a key wins, so the
	// local override comes first. The later file sees its values.
	values, err := dotenvx.Options{Env: []string{}}.Read("testdata/load/.env.local", "testdata/load/.env")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(values["HELLO"])
	fmt.Println(values["GREETING"])
}
Output:
local
Hello, local

Jump to

Keyboard shortcuts

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