.env.go.local File
# .env (shared) DB_HOST=localhost DB_PORT=5432 API_TIMEOUT=30s Use code with caution. Create a .env.go.local file for your private credentials:
With great power comes great responsibility. Here are the absolute, non-negotiable rules for handling .env files to keep your application and its data secure.
When this application runs, the .env.go.local file successfully overrides the database URL, API key, and log level while leaving the port number unchanged.
The godotenv library supports loading a variable list of files. If you want to mimic the .env.go.local pattern, you could call: .env.go.local
package main import ( "fmt" "log" "os" "://github.com" ) func init() // Order matters! godotenv.Load reads files from left to right. // However, it does NOT override variables that are already set. // To ensure .env.go.local takes priority, we load it first. files := []string".env.go.local", ".env" for _, file := range files if _, err := os.Stat(file); err == nil err := godotenv.Load(file) if err != nil log.Fatalf("Error loading %s file", file) func main() dbUser := os.Getenv("DB_USER") fmt.Printf("Running app with user: %s\n", dbUser) Use code with caution. Best Practices for .env.go.local
func main() // Load .env and then .env.go.local, allowing the latter to override _ = godotenv.Load(".env", ".env.go.local")
The file .env.go.local is a non-standard naming convention used for in Go projects . While Go developers standardly use .env or .env.local , adding .go to the filename usually serves to distinguish Go-specific configurations in polyglot (multi-language) repositories . Key Purpose of .env.go.local When this application runs, the
In practice, many Go libraries and frameworks support a family of environment files, with .env.local being one of the most common for providing local overrides. The presence of both .env and .env.local allows a project to have a set of sensible defaults (the base .env ) while still enabling each developer to apply their own customizations without interfering with the shared configuration.
As Go applications grow in complexity, managing configuration—database credentials, API keys, and environment-specific settings—becomes crucial. While environment variables are the standard, developers often need a way to manage local overrides without polluting the production environment or committing secrets to version control.
To implement this pattern, you need a configuration strategy. Go does not natively read .env files out of the box; it reads directly from the host system's environment. You must use a library to parse the file and inject it into the application runtime. 1. Create the Environment Files godotenv
: For developers frequently switching between branches (e.g., feature-a uses a different API key, bugfix-b uses a different database), swapenv is a lifesaver. This Go CLI tool lets you create and quickly switch between named .env configurations. You can create a "stripe" env, a "database" env, and swap them out without manually editing files or restarting your terminal.
In your main.go , load both files, ensuring the local one overrides the shared one:
In this scheme, your Go application would be configured to check first for variables in the system environment (highest priority), then in .env.go.local , and finally load any remaining defaults from .env or .env.go .
cp .env.example .env.go.local # They then edit .env.go.local with their real info.
External services / APIs