Load database URLs and API keys from a .env file in a Go app without hardcoding them.
Switch between development and production configs by swapping .env files with no code changes.
Prefix any shell command with .env file contents using the included CLI tool.
Load multiple .env files at once with existing environment variables taking priority.
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.
← joho on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.