.env.go.local
Create a script or Makefile target to help new developers:
init-dev:
cp .env .env.go.local.example
@echo "Created .env.go.local.example – copy to .env.go.local and edit"
Or in Go itself:
// cmd/setup/main.go (optional)
if _, err := os.Stat(".env.go.local"); os.IsNotExist(err)
sample := []byte("# Copy this to .env.go.local and edit\nDB_PASSWORD=changeme\n")
os.WriteFile(".env.go.local.example", sample, 0644)
fmt.Println("Created .env.go.local.example")
By following these practices and examples, you can effectively manage environment-specific configurations for your Go applications. .env.go.local
While .env.go.local is ignored by Git, never commit real secrets. Use a secrets manager (e.g., Vault, AWS Secrets Manager, 1Password CLI) in production, and keep local secrets out of version control entirely.
Ensure your .gitignore prevents accidental commits: Create a script or Makefile target to help
# Ignore all local overrides
*.go.local
This keeps production secrets and developer-specific paths out of version control.
To use the variables from .env.go.local in your Go application, you'll need a package to load the environment variables from the file. A popular choice is github.com/joho/godotenv. Here's a basic example of how to integrate it: Or in Go itself:
// cmd/setup/main
go get github.com/joho/godotenv
package main
import (
"log"
"github.com/joho/godotenv"
)
func main()
// Load environment variables from .env.go.local
err := godotenv.Load(".env.go.local")
if err != nil
log.Fatal("Error loading .env.go.local file")
// Accessing environment variables
dbHost := getenv("DB_HOST", "default_host")
log.Println("DB Host:", dbHost)
// Your application code here
func getenv(key, defaultValue string) string
val := os.Getenv(key)
if val == ""
return defaultValue
return val
Organize your project to separate shared configuration from local overrides:
/myapp
├── go.mod
├── main.go
├── config/
│ ├── config.go // Shared logic and defaults
│ └── env.go.local // Local overrides (ignored by git)
└── .gitignore