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

When importing, Go tools looks for the packages inside the $GOROOT
and $GOPATH/src
directories. These are simply environment variables and you can set them to any other path as well. But, you don’t need to.
$GOPATH
is called as the workspace directory for Go programs. Go source-code, belongs to you and to others lives here. So, when you import a package, Go searches that package inside this directory’s src directory.
You don’t need to set$GOROOT
variable either. $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
on my Mac, for example. You can change it to any other directory if you like but keep it there for now.
You can also provide more than one path to $GOPATH
environment variable. In Mac, for example, I can define more than one directory separated with colons like this: /path/1:path/2
By the way, you can see all of the environment variables Go uses by typing: go env
in your CLI. And then you can check whether those directories are existing or not. If they’re not, you can simply create them.

Alright, that’s all for now. Thank you for reading so far.
Let’s stay in touch:
- 📩 Join my newsletter
- 🐦 Follow me on twitter
- 📦 Get my Go repository for free tutorials, examples, and exercises
- 📺 Learn Go with my Go Bootcamp Course
- ❤️ Do you want to help? Please clap and share the article. Let other people also learn from this article.