Access Environment Variables in Go
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
Environment variables are an easy way to pass configuration settings to your program. This allows you to not store sensitive config settings like database passwords or API keys in your code or local files. This is the way Heroku passes any configuration variables to your application.
To read/write environment variables, Go has three functions in the os package.
os.Setenv sets a value of an environment variable by the key name. This is the signature of the function.
func Setenv(key, value string) error
os.Getenv is the complementary function which allows you to read any environment variable. It takes in a key name and returns back a string. Its upto you to convert into the correct datatype later.
func Getenv(key string) string
os.Unsetenv unsets an environment variable with a key name.
func Unsetenv(key string) error
Here is a very simple example code which sets an environment variable, prints it by Getting it and then unsets it before exiting.
package main
import (
"fmt"
"os"
)
func main() {
os.Setenv("TMPDIR", "/my/tmp")
defer os.Unsetenv("TMPDIR")
fmt.Println(os.Getenv("TMPDIR"))
}
What if you need to get back a slice of all environment variables? There is a handy os.Environ() function which does exactly that.
for _, s := range os.Environ() {
kv := strings.SplitN(s, "=", 2) // unpacks "key=value"
fmt.Printf("KEY:%q\tVAL:%q\n", kv[0], kv[1])
}
The variable s is a single string which has the key and value split by a = character (eg: EDITOR=/usr/bin/vim). We use the strings.SplitN function to parse it and store it in kv. Now this makes it easy to check only for a specific list of keys and get the values for those.