GOROOT is the directory where the Go toolchain is installed, GOPATH is the workspace directory where Go stores downloaded modules and installed tools (by default ~/go), and GOBIN is the optional folder where go install puts executables. Knowing GOPATH vs GOROOT saves time when a command is not found or a cache grows large.
Since Go modules became the default, you rarely edit any of these three variables. This guide explains what each one means, how to read it, and when changing it makes sense.
The go env command prints the settings the toolchain actually uses, which is more reliable than checking your shell variables.
The empty line is GOBIN: when it is unset, go install falls back to GOPATH/bin. To change a value permanently without editing shell files, use go env -w GOBIN=/home/you/bin, which writes to a per-user config file. Use go env -u GOBIN to remove the setting again.
GOROOT points to the Go distribution itself: the compiler, linker, go command and the standard library source.
The go binary finds its own GOROOT from its location, so you should not set it yourself. Setting it wrongly is a classic cause of errors such as "cannot find package fmt". Only change it if you intentionally keep a toolchain in an unusual location.
GOPATH defaults to $HOME/go (%USERPROFILE%\go on Windows). Before Go 1.11 all your code had to live in GOPATH/src. With modules, your projects can sit in any folder, and GOPATH is mainly a store:
| Path | Contents |
|---|---|
GOPATH/pkg/mod | The module cache: every dependency version you downloaded, read-only |
GOPATH/bin | Executables installed with go install (when GOBIN is not set) |
GOPATH/src | Only used by very old, pre-module projects |
You can list and clear the module cache with Go itself:
go clean -modcache deletes all downloaded modules; the next build downloads what it needs again. The build cache (compiled packages) is separate and found with go env GOCACHE.
GOBIN is the directory go install writes executables to. If it is empty, Go uses GOPATH/bin. Whichever folder applies must be on your PATH, otherwise the installed tool cannot be started by name.
On Linux and macOS add the line export PATH=$PATH:$(go env GOPATH)/bin to your shell profile. On Windows add the same folder through the system environment variables dialog.
| Variable | Answers the question | Default | Should you change it? |
|---|---|---|---|
GOROOT | Where is Go installed? | Install folder, for example /usr/local/go | Almost never |
GOPATH | Where do modules and tools go? | ~/go | Rarely, for example to use another disk |
GOBIN | Where do installed commands go? | GOPATH/bin | Sometimes, to share one tools folder |
A simple rule: GOROOT is Go, GOPATH is your downloads, and GOBIN is your commands.
In the old workflow each project had to be inside GOPATH/src/github.com/you/project, and dependencies were fetched into that tree with go get. Today a project has a go.mod file and can be in any folder. The old mode is switched off by default, and the GO111MODULE variable is no longer something a beginner needs. If you find tutorials that tell you to put code in GOPATH/src, treat them as outdated and use go mod init instead.
To make your own values permanent, prefer the Go tool. It stores them in a small per-user file, so they apply in every terminal and in editors such as VS Code.
A value set in a real environment variable takes priority over the file. If go env shows something unexpected, check whether your shell profile exports the same name. Remember to add the chosen bin folder to PATH afterwards.
No. It has a working default, ~/go. Set it only if you want the module cache and tools on a different drive.
It keeps every version of every dependency you ever built. Run go clean -modcache to reclaim space; Go downloads modules again when needed.
Yes. With modules, a folder with a go.mod can be anywhere on disk.
In GOBIN if set, otherwise in GOPATH/bin. Check go env GOBIN GOPATH and make sure that folder is on PATH.
GOROOT is the toolchain installation and normally needs no setting. GOPATH is the default ~/go store for the module cache and installed tools, and GOBIN overrides where go install places commands. Inspect all of them with go env and put the resulting bin folder on your PATH.