Effective way to read environment variables in Go

Avatar of Rakesh Mothukuri

Rakesh Mothukuri

| Reading Time : 2 minutes

Avatar of Rakesh Mothukuri Avatar of Rakesh Mothukuri Avatar of Rakesh Mothukuri Avatar of Rakesh Mothukuri

Introduction

One of the principles from the twelve-factor app is store application configuration in the environment because it is most likely application config will determine the variations of deployment to various regions such as staging, production, developer environments, etc

Configurations here are such as :

  1. Database, other supporting services URL’s
  2. Application secrets

GO provides a standard os package which consists of functions that can help to work with environment variables programmatically. Below are the functions that can be used to interact with environment variables

os.Setenv()

This function sets the value into the system environment, In order to set you to need to pass key and value pair of the environment variable, you need to set.

Example:

package main

import "os"

func main() {
  os.Setenv("API_KEY", "A8D8S9DS90D89SD09SD8S09D8S90A")
}

os.Getenv()

If you require to fetch the environment variables you would this, Parameter you need to pass is the key which in-turn fetches the value of the key if exists.

Example:

package main

import "os"

func main() {
  apiKey := os.Getenv("API_KEY")
}

os.Unsetenv()

This function is useful if you ever want to remove any environment variables from the system your application is running on. This parameter this function accepts the Key of the environment variable you need to remove.

Example:

package main

import "os"

func main() {
  os.Unsetenv("API_KEY")
}

os.ExpandEnv

ExpandEnv replaces ${var} or $var in the string according to the values of the current environment

Example:

package main

import (
    "fmt"
    "os"
)

func main() {
    os.Setenv("NAME", "gopher")
    os.Setenv("BURROW", "/usr/gopher")

    fmt.Println(os.ExpandEnv("$NAME lives in ${BURROW}."))

}
Output:
gopher lives in /usr/gopher.

os.LookupEnv()

This function gets the value environment variable named by the key, If the variable is not present in it returns empty and the boolean will be false. Otherwise, it returns the value (which can be empty), and the boolean is true.

Example:

package main

import (
    "fmt"
    "os"
)

func main() {
    show := func(key string) {
        val, ok := os.LookupEnv(key)
        if !ok {
            fmt.Printf("%s not set\n", key)
        } else {
            fmt.Printf("%s=%s\n", key, val)
        }
    }

    os.Setenv("SOME_KEY", "value")
    os.Setenv("EMPTY_KEY", "")

    show("SOME_KEY")
    show("EMPTY_KEY")
    show("MISSING_KEY")

}
Output:
SOME_KEY=value
EMPTY_KEY=
MISSING_KEY not set

os.Clearenv

This function removes all environment variables.

Example:

package main

import "os"

func main() {
  os.Clearenv()
}

os.Environ()

This function returns a copy of strings representing the environment, in the form “key=value”.