Fix your $GOPATH and $GOROOT

Don’t let $GOPATH and $GOROOT bite you.

When importing, Go tools searches for the packages inside $GOROOT and $GOPATH/src directories. These are the environment variables and you can set them to other paths, if you want.

$GOPATH is called as the workspace directory for Go programs. Go source-code, belongs to you and to others lives here. So, when you use import, Go looks inside this directory’s src directory to find the packages it searches for (which are to be imported to the current package).

You don’t need to set$GOROOT variable, it’s automatically set. $GOPATH is also set to a default directory, it’s set to your user’s home directory in your operating system. This defaults to ~/go in my Mac, for example. You can change it to any other directory if you like, but keep it there for now.

You can also give more than one path to $GOPATH as in $PATH environment variable. In Mac, for example, I can define more than one directory separated with colons like /path/1:path/2.

By the way, you can see all of the environment variables Go uses by typing: go env in your CLI.


To learn more about packages, see my other tutorials.

I’m also creating an online course for Go → Join to my newsletter

“ Let’s stay in touch weekly for new tutorials and tips “


My twitter — @inancgumus — I mostly tweet about Go.