Skip to content
package ... is not in GOROOT

package ... is not in GOROOT

DodaTech 2 min read

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 run or go build on a file outside a Go module.
  • The go.mod file is missing from your project.
  • The GOPATH environment 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.mod file.

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 myproject

Now 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/bin

Move your project to the proper location:

mkdir -p $GOPATH/src/myproject
cp -r /wrong/location/* $GOPATH/src/myproject/
cd $GOPATH/src/myproject

3. 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 + subdirectory

4. 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=on

5. 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.go

FAQ

What is the difference between GOROOT and GOPATH?
GOROOT is where Go itself is installed (the compiler and standard library). GOPATH is your workspace for Go projects. Since Go 1.16, modules have largely replaced GOPATH, and you can keep projects anywhere on your filesystem.
Why does Go look in GOROOT for my package?
Without a go.mod file, Go assumes you are using GOPATH mode. It searches GOROOT for standard library packages first, then GOPATH for your packages. If neither location contains the package, you get the “not in GOROOT” error.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro