LESSON · 1 OF 24

What environment variables hold

What environment variables hold

Environment variables are how the outside world hands configuration to a program without changing its code. A container runtime sets PORT, a CI system sets CI=true, a deploy pipeline sets DATABASE_URL. Your Go program reads them at startup and adjusts its behaviour.

The os package is the door to all of this. Everything a Go program needs to read, set, or list environment variables lives there, so any file that touches the environment starts with an import "os".

The next few nodes each take one function from that package: os.Getenv to read a value, os.LookupEnv to tell unset from empty, os.Setenv to set one, and os.Environ to list them all.

Spin up a fresh environment and practice live.
go · fresh machine · ready in under a minute