GOPATH and GOROOT in Go: What to Set and Why It Matters

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

GOPATH and GOROOT: What to Set and Why It Matters

\n\n

If you are just starting to learn Go, sooner or later you will come across the magic abbreviations GOPATH and GOROOT. The question "What should be the values of GOPATH and GOROOT?" is one of the most popular on Stack Overflow, and for good reason. Beginners get confused, set them incorrectly, and get strange errors when building projects.

\n\n

In this article, we will explain in simple terms what these variables are for, what default values to assign them, and how to switch to the modern approach with go modules. Let's go!

\n\n

What is GOROOT?

\n\n

GOROOT is the path to the folder where the Go compiler itself is installed. Inside this folder are the standard packages (fmt, net, os, etc.), as well as the binaries go, gofmt, and other utilities.

\n\n

Most developers do not need to explicitly set GOROOT. If you installed Go through the official installer or package manager, the compiler knows where it is.

\n\n

How to check the current GOROOT value?

\n

Open a terminal and run:

\n
go env GOROOT
\n\n

Example output on Linux:

\n
/usr/local/go
\n\n

On Windows (if installed to the default location):

\n
C:\\Program Files\\Go
\n\n

When should you change GOROOT?

\n
    \n
  • If you moved the Go folder to another location.
  • \n
  • If you have multiple Go versions installed and want to switch between them.
  • \n
  • If you compile Go from source and installed it to a non-standard directory.
  • \n
\n\n

In all other cases, leave GOROOT alone — let Go take care of it.

\n\n

What is GOPATH?

\n\n

Now, GOPATH is a much more interesting variable. Historically, Go required all your code to be in a single workspace. This workspace was defined by GOPATH.

\n\n

By default, GOPATH points to:

\n
    \n
  • Linux/macOS: $HOME/go
  • \n
  • Windows: %USERPROFILE%\\go
  • \n
\n\n

Inside this folder, the standard structure is:

\n
go/\n├── bin/        # compiled binaries\n├── pkg/        # built object files and cache\n└── src/        # source code of your projects\n    ├── github.com/\n    │   └── user/\n    │       └── myproject/\n    └── example.com/\n        └── myapp/
\n\n

How to check the current GOPATH value?

\n
go env GOPATH
\n\n

What should be in GOPATH and GOROOT? (Correct Values)

\n\n

Here is the short answer to the main question:

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
VariableDefault Value (Linux/macOS)Default Value (Windows)
GOROOT/usr/local/goC:\\Program Files\\Go
GOPATH$HOME/go%USERPROFILE%\\go
\n\n

Important: GOPATH and GOROOT must be different folders. Never set them to the same value!

\n\n

Common Mistakes and How to Fix Them

\n\n

Mistake 1: "GOPATH not set" or "cannot find package"

\n

If you are using the old approach (before versions 1.11-1.13), Go looks for packages inside $GOPATH/src. If you placed your project in a different location, the compiler won't find it.

\n\n

Solution:

\n
    \n
  • Either move the project to $GOPATH/src.
  • \n
  • Or

Blogs

Book Recommendations