Skip to main content

How to Read an Environment Variable in Go

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change between computers, such as a port, a database address or an API key, belong in environment variables. Go reads one with os.Getenv("NAME"), which returns an empty string when the variable isn't set.

Syntax​

Syntax
<value> := os.Getenv("<NAME>")
<value>, <ok> := os.LookupEnv("<NAME>")
PartWhat it does
os.GetenvReturns the variable's value, or "" when it isn't set.
os.LookupEnvAlso returns ok: true only when the variable is set, even to an empty value.
"<NAME>"The variable's name.

The os package documentation: "Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present. To distinguish between an empty value and an unset value, use LookupEnv."

The Example​

The example prints the value of a variable named GREETING in quotes, first with the variable set and then without it.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which reads os.Getenv("GREETING") into greeting and prints it in quotes with %q.

main.go
package main

import (
"fmt"
"os"
)

func main() {
greeting := os.Getenv("GREETING")
fmt.Printf("GREETING is: %q\n", greeting)
}

%q prints the string in quotes, so an empty string shows up as "" instead of nothing.

Step 2: Set the Variable and Run It​

Set GREETING to Hello and run the program. Then remove the variable and run it again:

Terminal
export GREETING=Hello
go run main.go
unset GREETING
go run main.go

A zsh terminal where export GREETING=Hello and go run main.go print GREETING is: "Hello", and after unset GREETING the same command prints GREETING is: "".

Output
GREETING is: "Hello"
GREETING is: ""

Without the variable, os.Getenv returned "". That's indistinguishable from a variable set to an empty value, which is where LookupEnv helps: for a variable that isn't set it returned "" and false.

Common Mistakes​

MistakeWhat happensDo this instead
Treating "" as "set"A missing variable silently becomes an empty settingCheck for "", or use LookupEnv
Using a number from the environment directlyIt's a string, such as "3000"Convert it with strconv.Atoi and check the error
Setting the variable in one terminal and running Go in another"": each terminal has its own variablesSet it in the terminal that runs the program

Troubleshooting​

SymptomLikely causeFix
The value is always ""The variable isn't set in this terminal, or the name differsSet it as in Step 2 and match the name
A required setting is missing but the program runs onThe empty string wasn't checkedStop with an error when the value is ""

FAQ​

Are variable names case-sensitive?​

On macOS and Linux, yes: GREETING and greeting are different variables. Windows treats them as the same.

How do I set a variable from Go?​

os.Setenv("NAME", "value") sets it for this program and the programs it starts.

Conclusion​

os.Getenv("NAME") returns a variable's value, or "" when it isn't set. Use os.LookupEnv when an empty value and a missing variable must be told apart.

Additional Resources​

  • os.Getenv, in the Go standard library documentation.
  • os.LookupEnv, in the Go standard library documentation.