Loading Application Config from Environment Variables
Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
Search for a command to run...
Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
No comments yet. Be the first to comment.
Goroutines are one of the best features of Go language. It is very easy to run a function as a goroutine, you just have to use the keyword go before the function call. Here is a very simple function which sleeps for n seconds and prints a line. pa...

In a previous post, we saw how we can use Regular expressions in Go to match and replace patterns in a string. While regexps are very useful, you might not want to them for every time you want to do some basic string manipulation. The strings package...

Go is a modern language, but doesn't use try-except blocks to handle errors. Errors are simple values that can be returned from functions and it is common to check for errors before proceeding with your next steps. Let's see the different ways to cre...

In the previous post, we saw how to write a simple unit test case and also how to write table driven test cases. In this post we will quickly see how to write benchmarks, as go supports running benchmarks on your functions as part of the standard tes...
Go has built-in support for writing test cases for your code. And it is important to write as much test cases as possible to make sure you cover all possible conditions. You have to use the testing standard package to write your unit test cases. Let...
Pro Golang Dev
14 posts
In a previous post, I had written how to read Environment Config. I had also mentioned how you can only store and retrieve string values. But if your application is trying to load config files from it, you would want to load the variables as integer or boolean values.
When I was new to go, I was using the strconv package to convert the string to various data types. Though it works, it isn't a clean way to write your code.
Later, I found the envconfig package which loads a config object from environment variables, and my life was changed. The way to use envconfig is really simple. You create a config structure with all the variables you want to load and create a variable of that struct. Then use the envconfig.Process function to load the environment variables into the config variable.
This is the signature of the Process function.
func Process(prefix string, spec interface{}) error
The prefix parameter is a string prefix you can use to maintain your environment variables without any namespace collisions with other programs running on that machine.
Here is a sample program which reads a few environment variables, loads them into a config structure having boolean, string and unsigned integer values.
package main
import (
"fmt"
"log"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
Debug bool
DatabaseURL string
Timeout uint
}
func main() {
var config Config
err := envconfig.Process("APP", &config)
if err != nil {
log.Fatal(err)
}
fmt.Println(config.Debug)
fmt.Println(config.DatabaseURL)
fmt.Println(config.Timeout)
}
If you run the program, you would just get back the default zero values for the different struct members.
$ go run envsample.go
false
0
But if you set the environment variables and run it again, you can get back the values. On a unix system, you can set the environment variables using the export command.
export APP_DEBUG=true
export APP_DATABASEURL=localhost:5432
export APP_TIMEOUT=60
Now running the program again, will give the output you were expecting.
$ go run envsample.go
true
localhost:5432
60
This is very useful when you are writing API servers or microservices which need to load a set of config and run in a docker container or PaaS like Heroku.