Use Dotenv Files When Developing Your Go Apps

Overcome the hurdles of managing environment variables, while developing your next Go app.


3 min read
One of the most widely searched for and linked to articles on this blog has become "Use Dotenv Files When Developing Your Python Apps". Having written those few paragraphs down last year, I had absolutely no idea that they would eventually become a go-to source for so many of you. It was only logical that I attempt to convey the same message to the Go community, a year after.

Hard-coding configuration is bad. This is one of the things that newbie programmers learn quickly after they realize that have committed sensitive information to a public GitHub repository. And it happens so easily. Often, you are in the spur of the moment, wanting to test something locally, when you figure out that you need some API key in order for your app to work. Adding the key to the code takes a second, and is just as easy to forget when committing. The Twelve-Factor-App methodology calls this a violation, and has whole section on why should be externalized and read from the environment at runtime.

I used to avoid using environment variables, because they made it hard to set up a local environment for testing. I’d either pass all the assignments at the start, or use a cumbersome startup script to export them to the current shell.

Luckily, it is easy to improve the quality of your code, and ease testing in different environments at the same time. It has become somewhat of a standard practice to use .env (a.k.a dotenv) files for configuring sensitive data while testing across environments. A dotenv file contains nothing more than text, where  it has one environment variable assignment per line:

ABC_KEY=1234xyz
DEF_KEY=2349875
DEBUG=True

NOTE: Dotenv files must be ignored from version control for exactly the same reason mentioned at the beginning of this post: exposing sensitive information as part of the project is bad. These files are only used to quickly set up or change an environment when needed.

Using a dotenv file in our app

How do we get our Go app to read a .env file? godotenv is a great Go package that does exactly that. It is one of the libraries I include in pretty much all of my Go projects. godotenv will search for a .env as part of the project, and if it finds one, will expose the variables in it to the app. All you need to do, is go get github.com/joho/godotenv and add the following couple of lines at the start of your project:

package main

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

func main() {
  err := godotenv.Load()
  if err != nil {
    log.Fatal("Error loading .env file")
  }

  s3Bucket := os.Getenv("S3_BUCKET")
  secretKey := os.Getenv("SECRET_KEY")

  // now do something with s3 or whatever
}

If you feel that calling godotenv.Load() is too much, you can even auto-load it:

import _ "github.com/joho/godotenv/autoload" 

Happy coding!

Use Dotenv Files When Developing Your Python Apps
Hard-coding configuration is bad. This is one of the things that newbie programmers learn quickly after they realize that have committed sensitive information to a public GitHub repository. And it happens so easily. Often, you are in the spur of the moment, wanting to test something locally, when yo…
The Twelve-Factor App
A methodology for building modern, scalable, maintainable software-as-a-service apps.
Package godotenv
Package godotenv is a go port of the ruby dotenv library (https://github.com/bkeepers/dotenv)

This Week in Elixir and Erlang #7
Previous article

This Week in Elixir and Erlang #7

A few links and news shared by the Elixir and Erlang community. No more, no less. Instead of a newsletter.

Farewell, macOS!
Next article

Farewell, macOS!

Last Friday, Apple released Big Sur, the latest version of macOS. While everyone else was busy downloading GBs of data from Apple’s servers, I


Related Articles

Go Makes Art
2 min read
This Week in Elixir and Erlang #9
5 min read
Elixir Community Voices: Lars Wikman
6 min read
This Week in Elixir and Erlang #8
3 min read
This Week in Elixir and Erlang #7
4 min read
This Week in Elixir and Erlang #6
3 min read

GO TOP

🎉 You've successfully subscribed to Preslav Rachev!
OK