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.
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
The os package provides functionality for interacting with the host environment’s operating system. The os package 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.
You can set environment variable key-value pairs with the Setenv method of the os package. The Setenv method takes in the pair and returns possible errors.
You can fetch environment variables by the keys (names) with the Getenv method. The Getenv method takes in the environment variable’s name and returns the value associated with the key.
The Environ method allows you to access all the environment variables on your host machine. The Environ method returns a slice of strings you can loop through and access the keys of environment variables.
The SplitN method of the strings package helps with splitting by a delimiter. In this case, it splits the variable name from the value.
How to Load Environment Variables From .env Files
The godotenv package is a Go port of the Ruby dotenv project for loading environment variables from the dotenv file.
The godotenv package provides functionalities for working with dotenv files over the os package, you can write and read from .env files.
Run this command to create a .env file 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 the godotenv package to read these environment variables in your Go program.
Run this command in the terminal of your project’s directory to install the godotenv package as a project dependency.
Import these packages into your Go file. You’ll use them along with the godotenv package to load environment variables and print them to the console or log errors.
You can load a .env file with the Load method of the godotenv package. The Load method takes in the file name and returns possible errors.
After loading the dotenv file, you can use the Getenv method of the os package to load the environment variables.
You can write to dotenv files with the Write method of the godotenv package. The Write method takes in a map of strings to strings and returns possible errors.
The godotenv package unmarshals the key-value pair string with the Unmarshal method, and the env variable becomes a map of string to string type.
The writeToDotEnv function writes the map’s contents to the .env file 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 can have multiple environment variables and dotenv files for varying scenarios to test how your application runs under varying parameters or conditions.