package ... is not in GOROOT
The “package … is not in GOROOT” error means Go is looking for your package inside the Go installation directory instead of your workspace. This happens when Go cannot find a go.mod file and falls back to the legacy GOPATH mode.
What It Means
In GOPATH mode, Go expects all source code to live under $GOPATH/src. When you try to build or run a file that imports a package outside this structure without a go.mod file, Go reports that the package is not in GOROOT because it searched the standard library location first and found nothing.
Why It Happens
- You are running
go runorgo buildon a file outside a Go module. - The
go.modfile is missing from your project. - The
GOPATHenvironment variable is not set, so Go defaults to GOROOT. - You are trying to import a local package without a module structure.
- The project was cloned without the
go.modfile.
How to Fix It
1. Initialize a Go module (preferred fix)
The modern solution is to use Go modules instead of GOPATH:
cd /path/to/your/project
go mod init myprojectNow Go will look for packages relative to the module path declared in go.mod instead of searching GOROOT.
2. Verify GOPATH is set correctly
If you must use GOPATH mode, ensure GOPATH is set:
echo $GOPATH
# If empty, set it:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/binMove your project to the proper location:
mkdir -p $GOPATH/src/myproject
cp -r /wrong/location/* $GOPATH/src/myproject/
cd $GOPATH/src/myproject3. Check your import paths
If you are using modules, verify that your import paths match the module name:
// go.mod
module github.com/user/myproject
// main.go
package main
import "github.com/user/myproject/mypackage" // must match module + subdirectory4. Remove GO111MODULE if set to off
If the environment variable GO111MODULE is set to off, Go forces legacy GOPATH behavior:
unset GO111MODULE
# Or set it to on:
export GO111MODULE=on5. Run from the module root
Ensure you run Go commands from the directory containing go.mod:
cd /path/to/project
ls go.mod
go run ./cmd/main.goFAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro