godotenv for Go: Complete Guide with Examples
\n\nThe godotenv library is a portable and lightweight tool for loading environment variables from .env files into the runtime environment of Go applications. It is inspired by the same-name library for Ruby and Node.js and follows the "12 Factor App" principle, which states that configuration should be stored in environment variables. godotenv allows developers to move sensitive data (API keys, database passwords, connection settings) out of the source code into a separate file that is not included in the version control system.
The main purpose of the library is to simplify configuration management during development. In a production environment, environment variables are usually set through system mechanisms (Docker, Kubernetes, CI/CD pipelines), but on a developer's local machine, it is convenient to use a .env file. godotenv automatically reads this file and makes all variables defined in it accessible through the standard os.Getenv() function. This eliminates the need to write your own parser and handle edge cases.
The library supports multi-line values, comments (lines starting with #), empty lines, and escaping of special characters. It has no external dependencies other than the Go standard library, making it an ideal choice for microservices and command-line utilities. godotenv does not override existing system environment variables, ensuring safe behavior during deployment.
Installation
\n\nTo install the library, run one of the following commands in the terminal at the root of your Go project:
\n\n# Install the latest stable version\ngo get github.com/joho/godotenv\n\n# If you are using Go Modules (recommended)\ngo get github.com/joho/godotenv@latest\n\n\nAfter running the command, a dependency entry will appear in the go.mod file. Make sure your project is initialized using go mod init.
Quick Start
\n\nCreate a .env file in the project root with the following content:
# .env file\nAPP_NAME=MySuperApp\nDB_HOST=localhost\nDB_PORT=5432\nDB_USER=admin\nDB_PASSWORD=secret123\nSECRET_KEY=my-super-secret-key\n\n\nNow write a simple Go program that loads this file and prints the variable values:
\n\npackage main\n\nimport (\n "fmt"\n "log"\n "os"\n\n "github.com/joho/godotenv"\n)\n\nfunc main() {\n // Load the .env file\n err := godotenv.Load()\n if err != nil {\n log.Fatal("Error loading .env file:", err)\n }\n\n // Read environment variables\n appName := os.Getenv("APP_NAME")\n dbHost := os.Getenv("DB_HOST")\n dbPort := os.Getenv("DB_PORT")\n dbUser := os.Getenv("DB_USER")\n dbPassword := os.Getenv("DB_PASSWORD")\n\n fmt.Printf("Application: %s\\n", appName)\n fmt.Printf("Database: %s:%s\\n", dbHost, dbPort)\n fmt.Printf("User: %s\\n", dbUser)\n fmt.Printf("Password: %s\\n", dbPassword)\n}\n\n\nRun the program:
\n\ngo run main.go\n\n\nYou should see output with data from your .env file.
Main Methods and Functions
\n\nThe godotenv library provides several key functions for working with configuration files. Let's look at each one in detail.
\n\n1. Load(filenames ...string) error
\n\nSignature: func Load(filenames ...string) error
Description: Loads variables from the specified files into the process environment. If no files are specified, it looks for a .env file in the current working directory by default. Returns an error if the file is not found.
Also in library
Working with CSV in Java: OpenCSV, Apache Commons CSV, and Java Scanner Libraries
Reqwest for Rust: Complete Guide with Examples
TypeScript Application Configuration: Libraries and Approaches
Monitoring Swift Applications: Libraries and Tools for iOS/macOS