Plurrrr

Mon 04 Jan 2021

Keep track of environment variables in go

When we configure our programs, we usually have three ways to do it: config files, cli flags and environment variables. While all of these three options are a solid way to go, with CI/CD environments you are usually instructed to use environment variables, e.g. envvars.

If we look at the golang’s os package documentation, we can see that it offers multiple ways to read envvars:

  • Environ which gives you raw list of strings in form of “key=value” which you can parse your self
  • Getenv which gives you the envvar value if it exists, or an empty string
  • LookupEnv which is similar to Getenv, but also returns you a boolean telling you if the variable exists or not

Source: Keep track of environment variables in go.