【问题标题】:"go build ./..." cannot find package“go build ./...” 找不到包
【发布时间】:2016-04-04 12:50:29
【问题描述】:

我知道互联网上到处都是类似的问题,我尝试了所有建议的解决方案,但至少 2 天无法弄清楚。

我尝试使用 IntelliJ IDEA 部署小型 Go 服务器,它运行良好,它可以构建/运行我的代码,但如果我使用终端作为示例:

go build ./...

我看到类似的东西:

OS-X-Dennis:backend denis$ go build ./...
out/production/mypocket_backend/server.go:4:2: cannot find package "api.jwt.auth/routers" in any of:
        /usr/local/go/src/api.jwt.auth/routers (from $GOROOT)
        /Users/denis/Programming/Golang/src/api.jwt.auth/routers (from $GOPATH)

OS X 10.11.2、IntelliJ IDEA 2016.1
$GOPATH = "用户/denis/Programming/Golang"
$GOROOT = "/usr/local/go"
$PATH = "$PATH:$GOPATH/bin"
这些路径正确吗?

这是我的工作结构:

/Golang/
 .bin/
 .pkg/
    .darwin-amd64/
         .//other folders/
 .src/
     .github.com/
     .backend/ //project's source
           /src
              /api.jwt.auth/
                //source code
           .server.go - file with main func
          //other files as example .gitignore
     .//other folders// 

这是我的主文件和整个项目结构的屏幕:

我看到日志显示了我的项目的错误路径。

 now
 /Users/denis/Programming/Golang/src/api.jwt.auth/routers
should
 /Users/denis/Programming/Golang/src/backend/src/api.jwt.auth/routers

我不知道我应该在哪里更正这条路径。

【问题讨论】:

  • 后端文件夹是您项目的来源还是那是什么?项目中包含的所有包都需要位于 GOPATH 的 src 文件夹中。我敢打赌,您的本地文件夹结构与服务器上的不同
  • 如何导入api.jwt.auth 包?我有一种强烈的感觉,你没有正确导入。
  • 应该是import "backend/src/api.jwt.auth",而不是api.jwt.auth
  • @nevets,我编辑了我的问题,我也编辑了我的导入,但没有帮助
  • @tier1,是的,它是一个源项目目录,所有需要项目的依赖项都包含在 GOPATH/src 中。

标签: intellij-idea go


【解决方案1】:

您的 GOPATH 中可以有多个目录。

如果你真的希望你的 backend 目录成为你的一些 Go 库的路径,除了你现有的 /Users/denis/Programming/Golang 路径之外,你可以将你的 GOPATH 设置为:/Users/denis/Programming/Golang:/Users/denis/Programming/Golang/src/backend

这将导致 import 语句在 /Users/denis/Programming/Golang/src 目录和 /Users/denis/Programming/Golang/src/backend/src 目录中搜索源文件。

我个人建议将您的 api.jwt.auth 文件夹移动到 /Users/denis/Programming/Golang/src/api.jwt.auth 以将您的源代码全部保存在一个区域中,但是如上所述在您的 GOPATH 中有两个不同的目录也可以。

要更好地理解 GOPATH,您只需输入go help gopath。还有一个快速入门是here

【讨论】:

  • 非常感谢,在 GOPATH 中使用 2 条路径的方法对我有帮助!这样做是不好的做法,我应该像你上面提到的那样移动我的源代码吗?
  • 我不认为这是不好的做法;只是不常见。
猜你喜欢
  • 1970-01-01
  • 2012-06-26
  • 2014-03-24
  • 2017-02-10
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多