go build_error ai_generated true

package example.com/user/project: cannot find package "example.com/user/project" in any of: /usr/local/go/src/example.com/user/project (from $GOROOT) /home/user/go/src/example.com/user/project (from $GOPATH)

ID: go/cannot-find-package-in-gopath

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-05-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.1 active
1.2 active
1.3 active
1.4 active
1.5 active
1.6 active
1.7 active
1.8 active
1.9 active
1.10 active
1.11 active
1.12 active
1.13 active
1.14 active
1.15 active
1.16 active

Root Cause

The package is not located in the expected GOPATH or GOROOT directories, often because the project was not cloned into the correct GOPATH structure or modules are not enabled.

generic

中文

包未位于预期的 GOPATH 或 GOROOT 目录中,通常是因为项目未克隆到正确的 GOPATH 结构中,或者未启用模块。

Workarounds

  1. 95% success Enable Go modules and initialize the module
    GO111MODULE=on go mod init example.com/user/project
  2. 85% success Move the project to the correct GOPATH location
    mkdir -p $GOPATH/src/example.com/user && mv /current/project $GOPATH/src/example.com/user/project

Dead Ends

Common approaches that don't work:

  1. Setting GO111MODULE=off and hoping GOPATH resolves 70% fail

    Disabling modules forces GOPATH mode, but if the project is not in the correct GOPATH src directory, it still fails.

  2. Copying the project to GOROOT/src 90% fail

    GOROOT is for the standard library; placing user projects there is unsupported and can cause conflicts.