explaingit

joho/godotenv

10,419GoAudience · developerComplexity · 2/5Setup · easy

TLDR

A Go library that loads key-value pairs from a .env file into environment variables, so you can configure your app differently for development and production without hardcoding settings.

Mindmap

mindmap
  root((godotenv))
    What it does
      Load env file
      Set env variables
      CLI tool included
    Load modes
      Explicit load call
      Autoload on import
      Multiple files
    Inspired by
      Twelve-factor app
      Ruby dotenv
    Status
      Feature-complete
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Load database URLs and API keys from a .env file in a Go app without hardcoding them.

USE CASE 2

Switch between development and production configs by swapping .env files with no code changes.

USE CASE 3

Prefix any shell command with .env file contents using the included CLI tool.

USE CASE 4

Load multiple .env files at once with existing environment variables taking priority.

Tech stack

Go

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

godotenv is a Go library that loads configuration values from a plain text file called .env into your program's environment variables. Environment variables are named settings that a program can read at runtime, commonly used to store things like database addresses, API keys, or passwords without hardcoding them into the source code. The .env file is a simple list of key-value pairs stored locally on your development machine and not checked into version control. The idea comes from the twelve-factor app methodology, a set of practices for building software that can be deployed in different environments (development, staging, production) without code changes. Rather than setting environment variables manually on every machine, you put them in a .env file and load it when the program starts. godotenv is a port of a Ruby library called dotenv that popularized this pattern, adapted for Go programs. The library can be used in a couple of ways. You can call a load function explicitly in your program to read the file, or you can import a special autoload package that reads the file automatically when the package is imported, before your main function runs. You can also load multiple .env files at once, with existing environment variables taking priority over ones defined in the files. There is also a command-line tool that lets you prefix any shell command with your .env file contents without writing Go code. The project has been declared feature-complete by the author, meaning new features will not be added. Bug fixes and compatibility improvements with the wider dotenv ecosystem are still accepted. The library is a Go port originally written by John Barton.

Copy-paste prompts

Prompt 1
Show me how to use godotenv to load a .env file at the start of my Go program and read a database URL from it.
Prompt 2
How do I use godotenv's autoload package so my .env file loads automatically before main() runs?
Prompt 3
I want to use godotenv's CLI tool to run my Go server with .env settings, show me the exact command.
Prompt 4
How do I load multiple .env files with godotenv and make sure existing environment variables take priority?
Open on GitHub → Explain another repo

← joho on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.