.env.go.local

package config

import ( "os" "strconv" )

type AppConfig struct Port int Debug bool DBURL string

// Load returns the configuration. Local file will augment this. func Load() AppConfig return AppConfig Port: getEnvAsInt("PORT", 8080), Debug: getEnvAsBool("DEBUG", false), DBURL: os.Getenv("DATABASE_URL"),

func getEnvAsInt(key string, fallback int) int if val, err := strconv.Atoi(os.Getenv(key)); err == nil return val return fallback

If you’ve built any non-trivial Go service, you’ve likely used a .env file. It’s the standard way to manage configuration during local development.

But as your system grows—adding message queues, caching layers, dependent APIs, or multiple developers—one .env file often becomes a source of friction.

Enter .env.go.local. It’s not a new standard. It’s a pattern. And it has saved my team from configuration hell more than once.

Never commit .env.go.local to GitHub, GitLab, or any version control system.

Open your .gitignore file and add the filename:

# .gitignore

The .env.go.local pattern is minimal, predictable, and requires no extra infrastructure. It respects the twelve-factor app principle while giving developers the flexibility they need for local work.

Give it a try on your next Go project – your teammates (and your future self) will thank you. .env.go.local


Would you like a ready-to-use config package example or a CLI tool to manage .env.go.local automatically?

Using .env.go.local for Local Development in Go Applications

As a Go developer, you're likely no stranger to managing environment variables in your applications. In a typical Go development workflow, you may have different environment variables for your local machine, staging, and production environments. Managing these variables can become cumbersome, especially when working on multiple projects simultaneously.

In this blog post, we'll explore how to use a .env.go.local file to simplify local development in Go applications.

The Problem with Environment Variables

Environment variables are a great way to decouple configuration from code, making your application more flexible and portable. However, managing environment variables can become a challenge, especially in local development.

Typically, you might have a .env file in your project's root directory that contains environment variables for your application. However, this file might not be suitable for local development, as you may need to override certain variables or add new ones specific to your local machine.

Introducing .env.go.local

To address this challenge, you can use a .env.go.local file in addition to your existing .env file. The idea is to create a separate file that contains local environment variables specific to your machine.

Here's an example of how you can structure your project:

my-go-app/
├── .env
├── .env.go.local
├── main.go
└── ...

In this example, the .env file contains environment variables that are shared across all environments, while the .env.go.local file contains local environment variables specific to your machine. package config import ( "os" "strconv" ) type

Loading Environment Variables

To load environment variables from both .env and .env.go.local files, you can use a library like github.com/joho/godotenv. Here's an example of how you can load environment variables in your Go application:

package main
import (
	"log"
"github.com/joho/godotenv"
)
func main() 
	// Load environment variables from .env and .env.go.local files
	err := godotenv.Load(".env", ".env.go.local")
	if err != nil 
		log.Fatal("Error loading environment variables:", err)
// Access environment variables
	log.Println("Local environment variable:", os.Getenv("LOCAL_VAR"))

In this example, the godotenv.Load function loads environment variables from both .env and .env.go.local files. If there are any duplicate variables, the values from .env.go.local will override those in .env.

Example Use Case

Let's say you're building a web application that uses a database. In your .env file, you have the following environment variables:

DB_HOST=localhost
DB_PORT=5432
DB_USER=myuser
DB_PASSWORD=mypassword

However, on your local machine, you want to use a different database instance with different credentials. You can create a .env.go.local file with the following contents:

DB_HOST=localdb
DB_PORT=5433
DB_USER=localuser
DB_PASSWORD=localpassword

When you run your Go application on your local machine, it will use the environment variables from both .env and .env.go.local files. The values from .env.go.local will override those in .env, so your application will use the local database instance with the specified credentials.

Best Practices

Here are some best practices to keep in mind when using .env.go.local:

Conclusion

Using a .env.go.local file is a simple yet effective way to manage local environment variables in your Go applications. By separating local environment variables from shared ones, you can simplify your development workflow and reduce the risk of configuration errors. // Load returns the configuration

Remember to follow best practices, such as keeping your .env.go.local file out of version control and using a consistent naming convention for your environment variables.

By adopting this approach, you can focus on building and testing your Go applications without worrying about environment variable management. Happy coding!

Since .env.go.local is not a standard, default file name in the Go ecosystem (unlike .env or .env.local), this guide assumes you are looking to implement a specific configuration pattern: Managing local environment variables for a Go application using a .env file.

This pattern is commonly used to load secrets (API keys, DB passwords) and configuration locally without hardcoding them or committing them to Git.

Here is a detailed guide on how to create, manage, and load a .env file (which we will refer to as .env.go.local for this specific workflow) in a Go project.


package config

import ( "log" "os" "github.com/joho/godotenv" )

func Load() // Load default .env first (if it exists) if err := godotenv.Load(".env"); err != nil log.Println("No .env file found, using system envs")

// Override with .env.go.local – fails silently if not present
_ = godotenv.Overload(".env.go.local")

func Get(key, defaultValue string) string if val := os.Getenv(key); val != "" return val return defaultValue