Simple Configuration Management in Go

Learn how to simplify configuration management in Go with the env library, including .env files, system variables, and easy environment switching.
Written by
Nathan Crocker
Published on
August 10, 2026

When building applications, certain tasks tend to reoccur. You’ll need a way to consistently log errors, monitor the health of your application, and — of course — manage configuration. Managing configuration efficiently, especially when it comes to environment variables, can streamline deployment, reduce manual effort, and simplify development processes.

In Go, environment variable management is critical for keeping your application flexible and portable across environments (development, staging, production). This is where the env library comes into play — a simple, intuitive library that makes managing environment variables in Go easy.

What Is env?

Well, besides a bland and unoriginal name? The env library is a lightweight Go package designed to manage environment variables effortlessly. It allows you to load environment variables from a .env file while also seamlessly falling back to system environment variables if needed. The goal is to make configuration management easy without the overhead of more complex solutions.

Key Features:

• Auto-loading from .env files: Automatically loads environment variables from a .env file located in the current directory.

• Fallback to system environment variables: If the requested variable isn’t found in the .env file, it checks the system’s environment variables.

• Simple API: Easy-to-use interface that integrates quickly into any Go project.

Getting Started

First, you’ll need to install the package into your Go project:

go get github.com/nathanbcrocker/env

Now let’s see how you can integrate it into your project.

Using env in Your Project

Here’s a simple example of how to use the env library:

package main

import (
 "fmt"
 "github.com/nathanbcrocker/env"
)
func main() {
 // Create a new environment manager
 e := env.NewEnv()
 // Get an environment variable
 port, ok := e.Get("PORT")
 if ok {
  fmt.Println("PORT:", port.Value)
 } else {
  fmt.Println("PORT not set")
 }
}

In this example:

  1. Creating an Env instance: First, you create an instance of the env manager using NewEnv().
  2. Loading environment variables: The library will automatically attempt to load variables from a .env file in your project directory.
  3. Fetching a variable: We call e.Get(“PORT”) to retrieve the PORT variable. If the variable is present in the .env file or system environment variables, it is printed; otherwise, the fallback message “PORT not set” is displayed.

How It Works

When you use the env library, it checks two places for environment variables:

  1. .env File: The .env file is commonly used to define environment-specific variables during local development. The env library loads these variables automatically, making it easier to switch between environments.
  2. System Environment Variables: If the variable isn’t found in the .env file, it falls back to the system’s environment variables. This is particularly useful when deploying to production environments where variables might be provided by the operating system or container.

Example .env File

Here’s a typical example of what your .env file might look like:

# .env file
PORT=8080
DEBUG=true

Why Use env?

The simplicity of the env library is one of its greatest strengths. It doesn’t come with unnecessary complexity, making it perfect for developers who want to manage configuration without adding extra overhead. Here’s why you might want to use it in your Go projects:

• Simplicity: With minimal setup, the library automatically loads configuration from both .env files and system environment variables.

• Portability: It makes it easy to switch between environments (development, staging, production) by changing only your .env file, keeping the codebase untouched.

• Convenience: No need to manage environment variables manually or write custom loaders for .env files.

Final Thoughts

Managing environment variables can be tricky, especially as applications scale and environments become more complex. With the env library, you can handle environment variables easily and consistently, allowing you to focus on building your application instead of worrying about configuration management.

By automating the loading of environment variables and providing a fallback system, env is a useful tool in the Go developer’s toolbox.

You can check out the source code for env and contribute to the project here: GitHub Repo.

Next Steps

  1. Try it in your project: You can start using env in your own Go projects and streamline your configuration management.
  2. Contribute, contribute, contribute! If you find bugs or want to enhance the functionality of env, feel free to contribute to the GitHub repository! There are some wonderful opportunities for expanding this: nested configurations, for example “DATABASE_URL=postgres://user:$PASS@localhost/dbname”, Cloud-based secret managers, just to name a few.
Newsletter
No spam. Just the latest releases and tips, interesting articles, and exclusive interviews in your inbox every week.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.