Effective way to read environment variables in Go

| Reading Time : 2 minutes | #backenddevelopment #Go


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”.


Articles from blogs I follow around the net

powerctl: A small case study in Hare for systems programming

powerctl is a little weekend project I put together to provide a simple tool for managing power states on Linux. I had previously put my laptop into suspend with a basic “echo mem | doas tee /sys/power/state”, but this leaves a lot to be desired. I have to u…

via Drew DeVault's blog August 28, 2022

United States v. Microsoft Corp exhibits

Links to exhibits from the Microsoft anti-trust case, with a bit of info on each link. Projection of PC marketshare Share of new browser users Share of the browser market, grouped by major ISP, 3 month moving average Share of the browser market, grouped by ma…

via danluu.com August 24, 2022

Status update, August 2022

Hi all! This month I’ve been pondering offline-first apps. The online aspect of modern apps is an important feature for many use-cases: it enables collaboration between multiple people and seamless transition between devices (e.g. I often switch between my pe…

via emersion August 14, 2022

Generated by openring

© Copyright 2021-2022. Rakesh Mothukuri