Question
I am trying to install doozer with this command:
goinstall github.com/ha/doozer
But I get errors like these:
goinstall: os: go/build: package could not be found locally
goinstall: fmt: go/build: package could not be found locally
goinstall: io: go/build: package could not be found locally
goinstall: reflect: go/build: package could not be found locally
goinstall: math: go/build: package could not be found locally
goinstall: rand: go/build: package could not be found locally
goinstall: url: go/build: package could not be found locally
goinstall: net: go/build: package could not be found locally
goinstall: sync: go/build: package could not be found locally
goinstall: runtime: go/build: package could not be found locally
goinstall: strings: go/build: package could not be found locally
goinstall: sort: go/build: package could not be found locally
goinstall: strconv: go/build: package could not be found locally
goinstall: bytes: go/build: package could not be found locally
goinstall: log: go/build: package could not be found locally
goinstall: encoding/binary: go/build: package could not be found locally
What should GOPATH and GOROOT be set to, and how are they supposed to work in Go?
Short Answer
By the end of this page, you will understand what GOROOT and GOPATH are, why Go needs them, how they differ, and how incorrect values can cause standard library packages like fmt or os to appear missing. You will also learn the modern context: when these variables matter, how to inspect them, and how to fix common setup problems.
Concept
GOROOT and GOPATH are environment variables used by Go to find code.
GOROOTpoints to the Go installation itself.GOPATHpoints to your workspace, where your own code and downloaded packages are stored.
What GOROOT means
GOROOT is the location where Go is installed. It contains:
- the Go compiler
- standard library source code
- standard library compiled packages
- Go toolchain binaries
If GOROOT is wrong, Go may fail to find built-in packages such as:
fmtosionet
That matches the kind of error shown in the question. If even standard packages cannot be found, the Go installation or GOROOT is likely incorrect.
What GOPATH means
GOPATH is a workspace directory for your own Go development. Historically, it was where Go expected to find:
Mental Model
Think of Go as having two homes:
GOROOTis Go's homeGOPATHis your workbench
GOROOT = the factory
This is where the language itself lives.
It contains the tools and built-in parts that ship with Go. If Go cannot find its own factory, it cannot even find basic parts like fmt or os.
GOPATH = your workshop
This is where you build your own things.
You keep your projects there, download dependencies there in older setups, and install tools there.
Simple analogy
Imagine a carpenter:
GOROOTis the tool factory that made the hammer and sawGOPATHis the carpenter's workshop where projects are built
If the factory address is wrong, you cannot even get a hammer. If the workshop address is wrong, your projects and downloaded materials go to the wrong place.
Syntax and Examples
In older Go setups, you might configure these environment variables like this.
Linux or macOS
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Windows
GOROOT=C:\Go
GOPATH=C:\Users\YourName\go
And add these to PATH:
C:\Go\bin
C:\Users\YourName\go\bin
Typical values
GOROOTis usually where Go was installed, such as/usr/local/goorC:\GoGOPATHis usually a directory you own, such as~/go
Check your setup
go env GOROOT
go env GOPATH
Example output:
Step by Step Execution
Consider this setup:
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Now imagine you run:
go env GOROOT
go env GOPATH
Step by step
-
GOROOTis read as/usr/local/go- Go looks there for its own installation.
- It expects standard library code and build tools there.
-
GOPATHis read as/home/yourname/go- Go treats this as your workspace.
- In older workflows, downloaded packages go under
src/, compiled artifacts underpkg/, and binaries underbin/.
-
When you run a build command
- Go first needs its own standard packages.
- It looks in
GOROOTfor packages like and .
Real World Use Cases
Even though Go modules changed the day-to-day workflow, these variables still matter in practice.
Diagnosing environment problems
If a developer machine or CI server cannot find Go packages, checking go env is one of the first debugging steps.
Installing command-line tools
Many Go-based tools install binaries into a location related to GOPATH, often:
$GOPATH/bin
This matters when a command installs successfully but cannot be run because its binary directory is not on PATH.
Working with older Go projects
Some legacy projects and setup guides still assume the GOPATH workspace model. Understanding it helps when maintaining older codebases.
CI/CD environments
Build servers may set custom Go paths. If those values are incorrect, builds can fail in ways that look confusing.
Multi-version Go setups
Developers using version managers or multiple Go installations sometimes accidentally point tools at the wrong GOROOT, causing mismatched builds or missing packages.
Real Codebase Usage
In real projects, developers usually rely on a few practical patterns.
1. Let Go manage GOROOT
In most modern environments, developers do not set GOROOT manually unless they have a special reason.
Why:
- manual configuration is easy to get wrong
- Go can usually detect its own install location
- incorrect
GOROOTcan break the standard library lookup
2. Use go env for debugging
A common debugging workflow is:
go env
This shows:
GOROOTGOPATH- module-related settings
- OS and architecture values
3. Keep GOPATH as a simple user-owned directory
Typical pattern:
/home/user/go
This avoids permission issues and keeps downloaded tools in one predictable place.
4. Add the binary directory to PATH
Common Mistakes
Here are the most common beginner mistakes with GOROOT and GOPATH.
Mistake 1: Setting GOROOT to the workspace directory
Broken example:
export GOROOT=$HOME/go
Why it breaks:
$HOME/gois usually your workspace- it is not the Go SDK installation
- Go cannot find standard packages there
Fix:
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
Or better: do not set GOROOT manually unless needed.
Mistake 2: Making GOPATH equal to GOROOT
Broken example:
export GOROOT=/usr/local/go
export GOPATH=/usr/local/go
Why it is a problem:
- it mixes the Go installation with your workspace
Comparisons
| Concept | Purpose | Typical Value | Common Mistake |
|---|---|---|---|
GOROOT | Location of the Go installation | /usr/local/go or C:\Go | Pointing it to your workspace |
GOPATH | Location of your workspace and installed tools | ~/go | Pointing it to the Go SDK |
PATH | Tells the shell where executables live | includes Go bin directories | Forgetting to include Go binaries |
GOROOT vs GOPATH
Cheat Sheet
GOROOT = where Go itself is installed
GOPATH = where your workspace and installed tools live
PATH = where your shell looks for executables
Typical values
GOROOT=/usr/local/go
GOPATH=$HOME/go
PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Useful commands
go env
go env GOROOT
go env GOPATH
which go
Key rules
- If standard packages like
fmtare missing, checkGOROOTfirst. GOPATHshould usually be a user-owned folder like~/go.- Do not set
GOPATHto the Go installation directory. - Do not set
GOROOTto your workspace directory. - In modern Go, you often do not need to set
GOROOTmanually.
Classic workspace layout
$GOPATH/
├── bin/
├── pkg/
└── src/
FAQ
What is the difference between GOROOT and GOPATH in Go?
GOROOT is the location of the Go installation. GOPATH is your workspace directory for projects and installed tools.
Do I need to set GOROOT manually?
Usually no. Modern Go installations normally detect GOROOT automatically.
What should GOPATH be set to?
A common choice is a directory you own, such as ~/go on Linux and macOS.
Why am I getting package could not be found locally for fmt or os?
Those are standard library packages. This usually means GOROOT is wrong or the Go installation is incomplete.
Can GOPATH and GOROOT be the same directory?
No. They serve different purposes and should not point to the same location.
Where do installed Go binaries go?
Commonly to , or another configured Go binary directory depending on your setup.
Mini Project
Description
Create a small Go environment checker script that helps diagnose common setup problems. This project is useful because environment issues are one of the first obstacles beginners face when installing Go packages or tools.
Goal
Build a simple Go program that prints GOROOT, GOPATH, and warns when the setup looks suspicious.
Requirements
- Read the
GOROOTandGOPATHenvironment variables. - Print both values clearly.
- Warn if either value is empty.
- Warn if
GOROOTandGOPATHare the same. - Print a helpful message explaining what each variable is for.
Keep learning
Related questions
Check if a Value Exists in a Slice in Go
Learn how to check whether a value exists in a slice in Go, and why Go has no Python-style `in` operator for arrays or slices.
Concatenating Slices in Go with append
Learn how to concatenate two slices in Go using append and the ... operator, with examples, pitfalls, and practical usage.
Convert String to Integer in Go: Idiomatic Parsing with strconv.Atoi
Learn the idiomatic way to convert a string to an int in Go using strconv.Atoi, with examples, errors, and common mistakes.