# Server configuration APP_PORT=8080 APP_DEBUG=true
I can provide a tailored to your exact architecture.
Every developer on a team has a unique local setup. One engineer might run Docker on port 5432, while another runs PostgreSQL natively on port 5433. A .env.go.local file allows each developer to fine-tune their local environment without altering the shared .env baseline template. Implementing .env.go.local in Go
: You should almost always add *.local or .env.go.local to your .gitignore file to ensure your private secrets never reach your shared repository.
# Docker run with environment variables docker run -e "APP_PORT=9090" -e "DB_HOST=postgres" myapp:latest .env.go.local
: Its primary purpose is to hold configurations specific to your local machine, such as local database credentials, private API keys, or unique file paths.
The running service will use LOG_LEVEL=debug and REDIS_ADDR=localhost:6380 , but still take PORT and DB_DSN from .env .
One standout feature of this library is its for JSON files. Nested JSON objects are automatically transformed into environment variables with underscore delimiters:
Becomes: DATABASE_PRIMARY_HOST=localhost and DATABASE_PRIMARY_PORT=5432 . err := os.Stat(".env.go.local")
REDIS_ADDR=localhost:6380 LOG_LEVEL=debug
If you are currently setting up a Go environment, let me know:
. The order of loading determines the variable precedence. We'll load the base file first, then the local one.
: As we've touched on, the most critical rule is to never commit sensitive information like passwords, API keys, or tokens to version control . A file like .env.go.local , which is typically added to your .gitignore file, is not tracked by Git. It acts as a safe vault for your local secrets, ensuring they stay on your machine and don't end up in a public code repository. Your database.go file can freely call os.Getenv("DB_PASSWORD") , knowing that secret will be safely injected at runtime, not stored in plain sight in your codebase. err == nil if _
: The baseline configuration file. It contains default values shared across the entire engineering team. This file is safe to commit to Git.
Because .env.go.local is ignored by Git, fresh clones of your repository will lack the variables needed to compile or run the application. To solve this, create a tracked boilerplate file named .env.go.local.template (or .env.example ). This file defines the required keys but leaves the sensitive values blank:
godotenv is the most widely used library for loading environment files in Go. It's lightweight, well-tested, and handles the common cases brilliantly.
External services / APIs
// Optional: warn if .env is missing but .env.go.local exists if _, err := os.Stat(".env.go.local"); err == nil if _, err := os.Stat(".env"); err != nil log.Println("Warning: .env.go.local exists but no .env - defaults missing")