“go build ./…” 无法找到包。

huangapple go评论83阅读模式
英文:

"go build ./..." cannot find package

问题

我知道互联网上有很多类似的问题,并且我尝试了所有建议的解决方案,但是至少两天来我都无法解决它。

我正在尝试部署一个小的Go服务器,我使用的是IntelliJ IDEA,它工作得很好,可以构建/运行我的代码,但是如果我在终端中使用如下命令:

go build ./...

我会看到类似以下的内容:

OS-X-Dennis:backend denis$ go build ./...
out/production/mypocket_backend/server.go:4:2: 找不到包 "api.jwt.auth/routers",在以下任何位置都找不到:
/usr/local/go/src/api.jwt.auth/routers (来自 $GOROOT)
/Users/denis/Programming/Golang/src/api.jwt.auth/routers (来自 $GOPATH)

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

这是我的工作结构:

/Golang/
.bin/
.pkg/
.darwin-amd64/
//其他文件夹/
.src/
.github.com/
.backend/ //项目的源代码
/src
/api.jwt.auth/
//源代码
.server.go - 包含主函数的文件
//其他文件,例如.gitignore
//其他文件夹//

这是我的主文件和整个项目结构的截图:
“go build ./…” 无法找到包。

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

现在
/Users/denis/Programming/Golang/src/api.jwt.auth/routers
应该是
/Users/denis/Programming/Golang/src/backend/src/api.jwt.auth/routers

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

英文:

I know internet full of similar issues and I tried all of suggested solutions, but can't figure out it at least 2 days.

I trying to deploy little Go server I using IntelliJ IDEA and it works great, it can build/run my code, but if I use terminal as example:

go build ./...

I see something like that:

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<br>
$GOPATH = "Users/denis/Programming/Golang"<br>
$GOROOT = "/usr/local/go"<br>
$PATH = "$PATH:$GOPATH/bin"<br>

These paths are correct? <br>

Here is my work structure:

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

Here is my screen of main file and whole project-structure:
“go build ./…” 无法找到包。

I see that logs show me wrong path to my project.

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

I don't know where I should correct this path.

答案1

得分: 1

你的GOPATH可以包含多个目录。

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

这将导致导入语句在/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。此外,这里有一个快速入门指南。

英文:

Your GOPATH can have multiple directories in it.

If you truly want your backend directory to be a path of some of your Go libraries, in addition to your existing path of /Users/denis/Programming/Golang you can set your GOPATH to: /Users/denis/Programming/Golang:/Users/denis/Programming/Golang/src/backend

This will cause import statements to search for source files in both the /Users/denis/Programming/Golang/src directory and the /Users/denis/Programming/Golang/src/backend/src directory.

I would personally recommend moving your api.jwt.auth folder to /Users/denis/Programming/Golang/src/api.jwt.auth to keep your source all in one area, but having two different directories in your GOPATH as suggested above will work too.

To understand GOPATH better, you can simply type go help gopath. Also a quickstart is here

huangapple
  • 本文由 发表于 2016年4月4日 20:50:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/36403187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定