What are GOROOT and GOPATH in Go?

The most frequently asked question learning Go

Lakhan Saiteja
3 min readDec 31, 2021

If you are new to Go, chances are you’ve heard of GOROOT and GOPATH and might be wondering what they are or maybe what’s the difference between them. You’re not alone! This is one of the most frequently asked questions about Go. I had this question too when I started to learn Go.

This is a beginner’s guide that should answer all your questions about GOROOT and GOPATH.

Golang image with gopher

So what are they anyway?

  • For starters, both GOROOT and GOPATH are environment variables that point to two directories in your system — be it Windows, Mac or Linux.
  • Go compiler uses these variables for building and running Go programs.
  • When you install Go, they are set to default values. You can however change them (explained in the later section).
  • On MacOS, the default value for GOROOT is /usr/local/go (or /usr/local/Cellar/go if you installed Go using HomeBrew). The default value for GOPATH is $HOME/go.

How are GOROOT and GOPATH different from each other?

While they might seem similar, GOROOT and GOPATH have different purposes.

GOROOT

  • GOROOT is the installation directory for Go on your system.
  • This is where Go compiler finds the system packages for Go like fmt, strings, net/http. The packages that come, by default with Go.
  • This is also where the Go binary used for running Go programs, is located.

GOPATH

  • GOPATH is a directory where Go compiler installs and imports additional Go packages (or libraries)— say from Github.
  • Unlike few other programming languages where you can import packages from any directory (and have your source code anywhere on the system), Go expects all the packages and source code to be in the GOPATH directory (exception is when you use Go Modules which I’ll cover in detail in a later post).
  • When you run go get or go install, new packages are installed in the GOPATH directory.

Example:

When you run the command below -

go install github.com/gin-gonic/gin@master

Go installs gin (a http web framework for Go) package from Github in GOPATH. More specifically, gin package would be installed in the directory — $GOPATH/src/github.com/gin-gonic/gin

Later when you import gin in a Go program like below —

import "github.com/gin-gonic/gin"

gin package from above directory (in GOPATH) is used.

Documentation on GOPATH can be found here.

How to view current GOROOT and GOPATH?

Once you’ve installed Go, you can check your current GOROOT and GOPATH using the command below:

go env | grep -E 'GOPATH|GOROOT'
terminal output showing GOROOT and GOPATH variables

As seen from the snapshot above, in my system -

GOROOT points to /usr/local/go directory and

GOPATH points to /Users/lakhansaiteja/coding/golang/gopath

It might be different from the GOROOT and GOPATH values in your system because I’ve changed GOPATH to another directory where all my Go source code is located.

How to change GOROOT and GOPATH?

One of the easiest ways to change GOROOT and GOPATH is to export these environment variables to the desired path in your zsh (or bash) profile like below -

export GOPATH=/Users/lakhansaiteja/coding/golang/gopath

Source your zsh (or bash) profile after the export to apply the changes.

If we’re meeting for the first time, Hey there! I’m Lakhan. I’m a Software Engineer passionate about Distributed Systems, Cloud, Kubernetes & Golang. I love writing about them too.

Thanks for reading. Until next time. Ciao!

--

--