Working With Environment Variables in Go

Environment variables are dynamically named key-value pairs set aside in the host environment used to affect the running operations of programs.

Environment variables are popular for interacting with application-sensitive data as they provide an abstraction over the interface of the environment. You can use environment variables to increase your applications’ security.

4

Go provides built-in support for working with environment variables, and there are many packages for working with environment variables and environment variable files (.env) in the Go ecosystem.

Environment Variables and the os Package

Theospackage provides functionality for interacting with the host environment’s operating system. Theospackage provides methods for setting and retrieving environment variable key-value pairs.

Import these packages for setting, loading, and printing the environment variables on your host machine.

Using hidden Windows menu

You can set environment variable key-value pairs with theSetenvmethod of theospackage. TheSetenvmethod takes in the pair and returns possible errors.

You can fetch environment variables by the keys (names) with theGetenvmethod. TheGetenvmethod takes in the environment variable’s name and returns the value associated with the key.

mega cloud storage with mega logo on laptop screen.

TheEnvironmethod allows you to access all the environment variables on your host machine. TheEnvironmethod returns a slice of strings you can loop through and access the keys of environment variables.

TheSplitNmethod of the strings package helps with splitting by a delimiter. In this case, it splits the variable name from the value.

A computer monitor showing the Windows 11 security settings

How to Load Environment Variables From .env Files

Thegodotenvpackage is a Go port of the Ruby dotenv project for loading environment variables from the dotenv file.

Thegodotenvpackage provides functionalities forworking with dotenv filesover theospackage, you can write and read from.envfiles.

man sitting in front of three laptops

Run this command to create a.envfile in your working directory.

The touch command is used to create new files. Add the following lines of code to the dotenv file. You’ll use thegodotenvpackage to read these environment variables in your Go program.

Run this command in the terminal of your project’s directory to install thegodotenvpackage as a project dependency.

Import these packages into your Go file. You’ll use them along with thegodotenvpackage to load environment variables and print them to the console or log errors.

You can load a.envfile with theLoadmethod of thegodotenvpackage. TheLoadmethod takes in the file name and returns possible errors.

After loading the dotenv file, it’s possible to use theGetenvmethod of theospackage to load the environment variables.

You can write to dotenv files with theWritemethod of thegodotenvpackage. TheWritemethod takes in a map of strings to strings and returns possible errors.

Thegodotenvpackage unmarshals the key-value pair string with theUnmarshalmethod, and theenvvariable becomes a map of string to string type.

ThewriteToDotEnvfunction writes the map’s contents to the.envfile in the working directory. This operation overwrites the existing data.

Environment Variables Always Come in Handy

Environment variables make it easy to set and change the application’s execution parameters without altering code or configurations, thereby increasing the portability of your application.

you could have multiple environment variables anddotenvfiles for varying scenarios to test how your application runs under varying parameters or conditions.

Every now and then I’ll learn a little tip that makes me think “well, if I known that a year ago then it’d have saved me hours of time”. I vividly remember learning how to use the copy and paste functions, all on my own, as a kid. It was like the entire Internet had just become twice as easy for me. Environment variables are a little-known detail of Windows, if you’re a novice user. They belong in this same realm of time-saving conversation.

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

Not Linux, not Windows. Something better.

When your rival has to bail out your assistant.

Don’t let aging hardware force you into buying expensive upgrades.

Don’t let someone else take over your phone number.

Technology Explained

PC & Mobile