英文:
"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
//其他文件夹//
我看到日志显示了错误的项目路径。
现在
/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'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:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论