go build_error ai_generated true

包 example.com/user/project:在任何位置都找不到包 "example.com/user/project": /usr/local/go/src/example.com/user/project(来自 $GOROOT) /home/user/go/src/example.com/user/project(来自 $GOPATH)

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

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-05-20首次发现

版本兼容性

版本状态引入弃用备注
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

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

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

    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% 失败

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