How to properly check environment variables values in Go
One thing I’ve found fairly often in Go programs is people checking if an environment variable is defined by using the os.LookupEnv
function, that returns a string with the value, and a boolean indicating that the variable is present. However, what most people seem to miss is the comment that the returned value might still be empty!
Take for instance the following program that you can try in the Go Playground:
package main
import (
"fmt"
"os"
)
func main() {
os.Setenv("FOO", "bar")
printEnv("FOO")
os.Setenv("FOO", "")
printEnv("FOO")
os.Unsetenv("FOO")
printEnv("FOO")
}
func printEnv(v string) {
ge := os.Getenv(v)
le, ok := os.LookupEnv(v)
fmt.Printf("Getenv(%[1]q) => %[2]q\nLookupEnv(%[1]q) => (%[3]q, %[4]t)\n\n", v, ge, le, ok)
}
If you run this, the results might not be what you were expecting:
Getenv("FOO") => "bar"
LookupEnv("FOO") => ("bar", true)
Getenv("FOO") => ""
LookupEnv("FOO") => ("", true)
Getenv("FOO") => ""
LookupEnv("FOO") => ("", false)
Now you know: the proper way to check an environment variable os it use os.LookupEnv
and check both the boolean and that the string isn’t empty, otherwise you might introduce a bug in your program.
PS: how cool is that you can refer to arguments in a formatting string using their position?