GoDotEnv ports Ruby’s dotenv behavior to Go, reading key-value pairs from .env files into the process environment. It works as an importable package or a small command-line helper.
Overview
GoDotEnv loads configuration from .env files into environment variables when a Go program or shell command starts, following the twelve-factor idea of keeping deployment-specific settings out of source code. It is a Go port of the Ruby dotenv project and can serve daemons as a library or wrap arbitrary commands via its binary.
Typical use adds variables to a root .env file, then calls Load near program entry or imports the autoload package to read .env on import. Multiple files, non-default paths, maps via Read or Parse, and writing Marshal output back to disk are supported. Syntax allows comments, exports, inline comments, and YAML-like key colon forms.
Later-loaded values do not override variables already set in the environment unless you deliberately use Overload. A common pattern chains .env files by application environment name. The command-line mode loads a chosen file then execs another program, with an optional flag to override existing variables.
Key names allow the same character set as Node’s dotenv, including hyphens, which differs from Ruby’s stricter set. Continuous integration covers Linux-style and Windows environments for the library, though Windows behavior of the binary is not guaranteed.
Key features
- Load and Overload from .env files
- Autoload package on import
- Parse from files, readers, or strings
- Marshal and Write env maps to disk
- Command wrapper with optional override
- Multi-file and environment-specific chains
- Comments and export syntax in files
Use cases
- Local development configuration in Go
- Injecting secrets before running commands
- Layered .env files per environment name
- Round-tripping env maps to disk
- Twelve-factor config without hardcoding