
GoDotEnv
A Go library and command that loads variables from a .env file into the environment.
- Stars
- 10.7k
- Forks
- 469
- Licence
- MIT
- Language
- Go
- Last update
- 3 months ago
- Type
- Library
What it is
godotenv is a Go port of Ruby's dotenv project. It reads key=value pairs from a .env file and sets them as environment variables, so an app can keep settings like database addresses or credentials outside its code. It can be imported as a Go library or run as a command-line tool.
- Load S3_BUCKET and SECRET_KEY from a .env file at startup with godotenv.Load()
- Read a .env file into a map with godotenv.Read() without touching the process environment
- Load per-environment files such as .env.development or .env.local in a defined order
- Run a command with variables from a file using the godotenv command with the -f flag
Great for
- Go developers who want local configuration kept in a .env file
- Teams following twelve-factor style app configuration
- Developers who want to read .env contents into a map without changing the environment
- Projects that run a command with variables loaded from a file
Not ideal for
- Projects not written in Go, since this is a Go package (the README does not mention other languages)
- Anyone needing a guarantee the command-line tool works on Windows, because the README says it makes no guarantees for that
How to get it
- 1
Install with a package manager
You'll need: Go (version 1.24 or newer is required only for the go get -tool form in the README)
Install as a library
go get github.com/joho/godotenvInstall the command-line tool
go install github.com/joho/godotenv/cmd/godotenv@latest - 2
Run it
- Create a .env file in your project root with lines like S3_BUCKET=YOURS3BUCKET
- Add a Go file that imports github.com/joho/godotenv and calls godotenv.Load()
- Call os.Getenv("S3_BUCKET") to read the value
- Run your Go program as usual
- 3
Get the most out of it
- Use godotenv.Overload() only when you need to replace existing variables, as the README advises caution
- Use the autoload package (import _ "github.com/joho/godotenv/autoload") to load .env on import
- Load several files in order, such as .env, .env.<env>, and .env.local, following the README's convention
- Use godotenv.Unmarshal or godotenv.Marshal to work with env content as strings
Downloads
Strengths
- Simple API: a single Load call covers the common case
- Supports comments, export prefixes, and YAML-style key: value lines, per the README
- Includes functions to parse, write, and marshal env content, not just load it
Trade-offs
- The README says the library is feature complete and will not accept new functionality or breaking API changes
- No stated guarantee that the command-line binary works on Windows
- Variable name rules differ from Ruby's dotenv, so some keys valid in Node fail under Ruby
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| godotenv (this) | 10.7k | Library | beginner | 3 months ago | Go developers who want local configuration kept in a .env file |
| viper | 30.5k | Library | intermediate | 10 months ago | Go developers building command-line tools or servers that need configuration |
| go-zero | 33.4k | Library | intermediate | 5 days ago | Go developers building REST APIs or microservices |
| kit | 27.4k | Library | advanced | 2.3 years ago | Go developers building service-oriented or microservice backends |
| kratos | 26k | Library | intermediate | 5 days ago | Go developers building a set of backend services that talk over HTTP or gRPC |
| zap | 24.7k | Library | beginner | 2 months ago | Go developers building services that log heavily |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.