How to Read an Environment Variable in Go
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
<value> := os.Getenv("<NAME>")
<value>, <ok> := os.LookupEnv("<NAME>")
| Part | What it does |
|---|---|
os.Getenv | Returns the variable's value, or "" when it isn't set. |
os.LookupEnv | Also 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 runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named 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:
- macOS / Linux
- Windows (PowerShell)
export GREETING=Hello
go run main.go
unset GREETING
go run main.go

$env:GREETING = 'Hello'
go run main.go
Remove-Item Env:GREETING
go run main.go

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
| Mistake | What happens | Do this instead |
|---|---|---|
Treating "" as "set" | A missing variable silently becomes an empty setting | Check for "", or use LookupEnv |
| Using a number from the environment directly | It'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 variables | Set it in the terminal that runs the program |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The value is always "" | The variable isn't set in this terminal, or the name differs | Set it as in Step 2 and match the name |
| A required setting is missing but the program runs on | The empty string wasn't checked | Stop 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.
