GOPATH on Ubuntu: How Not to Break Your Brain (and Code)
You've probably encountered this: you just installed Go on Ubuntu, open a terminal, type go run main.go, and get silence in response. Or, even worse, a sea of red errors mentioning GOPATH. If you've Googled this problem, you've almost certainly seen that very question on Stack Overflow with thousands of views: "How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?". Sound familiar? Welcome to the club.
I once spent half a day trying to figure out why Go couldn't see my packages, even though I "did everything right." Spoiler: I did it wrong. In this article, we'll break down the top errors when configuring GOPATH on Ubuntu, learn how to avoid them, and finally set up the environment so it works like clockwork.
What is GOPATH and Why Is It Still Important?
Before diving into the details, let's recall what GOPATH is. In the Go world (especially versions before 1.16), this was a sacred cow. GOPATH is the path to the Go working directory where your source code, compiled binaries, and third-party packages are stored. By default on Ubuntu, it is set to $HOME/go, but many developers (especially those working with multiple projects) change it.
With the advent of Go Modules (starting from Go 1.11), the need for manual GOPATH management decreased, but it didn't disappear entirely. If you work with older projects, use go get, or simply want go install to place binaries in a convenient location, you'll still need to get acquainted with this variable.
Top 5 Errors When Configuring GOPATH on Ubuntu
I've compiled the most common pitfalls that beginners (and not only) stumble upon. Check if you're currently lying on one of these bumps.
Error #1: Editing the Wrong File
The most popular question on Stack Overflow. Users dive into /etc/environment, /etc/profile, or even /etc/bash.bashrc. Yes, it will work, but it's bad practice. System files are for system settings. Your GOPATH is a personal variable, and its place is in user configuration files.
How to do it correctly: edit ~/.bashrc (for Bash) or ~/.profile (if you use a login shell). If you're on Zsh, it's ~/.zshrc.
Error #2: Forgetting the Export
I often see in configs: GOPATH=$HOME/go without export. Without export, the variable won't be passed to child processes (including your IDE and go build).
Correct syntax:
export GOPATH=$HOME/goexport PATH=$PATH:$GOPATH/binError #3: Not Reloading the Terminal
You edited .bashrc, saved it, and then sit staring at an empty echo $GOPATH. No magic will happen until you apply the changes. The command source ~/.bashrc is your best friend.
Error #4: Conflict with Go Modules
If you use go mod init for each project, GOPATH can interfere. Especially if you try to work inside $GOPATH/src. Modern Go doesn't require this, but if you insist on placing your project in ~/go/src/myproject, be prepared for unexpected errors.
Error #5: Spaces and Special Characters in the Path
Ubuntu allows creating folders with spaces. Don't do this for Go. /home/user/My Projects/go is a headache. Use /home/user/projects/go or simply $HOME/go.
Step-by-Step Guide: How to Configure GOPATH Correctly
Let's do this once and for all. Open a terminal and execute the commands sequentially.
Step 1. Check Your Current Go Version
go version