英文:
can't load package: package .: no buildable Go source files
问题
这是错误信息:
% go get
无法加载包:包 .: 在 /Users/7yan00 中没有可构建的 Go 源文件
% echo $GOPATH
/Users/7yan00/Golang
你会如何解决这个错误?
英文:
Here is the error message:
% go get
can't load package: package .: no buildable Go source files in /Users/7yan00
% echo $GOPATH
/Users/7yan00/Golang
How would you troubleshoot that error?
答案1
得分: 54
确保你在Go项目源文件夹中使用该命令(例如/Users/7yan00/Golang/src/myProject
)。
一个替代方案(类似于这个bug)是使用-d
选项(参见go get
命令)
go get -d
-d
标志指示get在下载包后停止;也就是说,它指示get不安装包。
看看这是否对你有帮助。
但更一般地,如此线程所述:
go get
是用于包,而不是用于存储库。所以如果你想要一个特定的包,比如
go.text/encoding
,那么使用
go get code.google.com/p/go.text/encoding
如果你想要该存储库中的所有包,使用
...
表示:
go get code.google.com/p/go.text/...
英文:
Make sure you are using that command in the Go project source folder (like /Users/7yan00/Golang/src/myProject
).
One alternative (similar to this bug) is to use the -d
option (see go get
command)
go get -d
> The -d
flag instructs get to stop after downloading the packages; that is, it instructs get not to install the packages.
See if that helps in your case.
But more generally, as described in this thread:
> go get
is for package(s), not for repositories.
>
> so if you want a specific package, say, go.text/encoding
, then use
go get code.google.com/p/go.text/encoding
> if you want all packages in that repository, use ...
to signify that:
go get code.google.com/p/go.text/...
答案2
得分: 32
你应该检查$GOPATH
目录。如果包名的空目录存在,go get
不会从代码库下载该包。
例如,如果我想从github.com/googollee/go-socket.io
的代码库获取包,而$GOPATH
中已经存在一个空目录github.com/googollee/go-socket.io
,go get
不会下载该包,并会抱怨目录中没有可构建的Go源文件。首先删除任何空目录。
英文:
You should check the $GOPATH
directory. If there is an empty directory of the package name, go get
doesn't download the package from the repository.
For example, If I want to get the github.com/googollee/go-socket.io
package from it's github repository, and there is already an empty directory github.com/googollee/go-socket.io
in the $GOPATH
, go get
doesn't download the package and then complains that there is no buildable Go source file in the directory. Delete any empty directory first of all.
答案3
得分: 13
另一个可能导致该消息的原因是:
> 无法加载包:....:没有可构建的Go源文件
当编译的源文件中包含以下内容时:
// +build ignore
在这种情况下,这些文件将被忽略,并且无法按要求构建。这种行为在https://golang.org/pkg/go/build/中有记录。
英文:
Another possible reason for the message:
> can't load package: .... : no buildable Go source files
Is when the source files being compiled have:
// +build ignore
In which case the files are ignored and not buildable as requested.This behaviour is documented at https://golang.org/pkg/go/build/
答案4
得分: 6
为了解决我的情况,我需要指定一个更具体的子包进行安装。
错误的方式:
go get github.com/garyburd/redigo
正确的方式:
go get github.com/garyburd/redigo/redis
英文:
To resolve this for my situation:
I had to specify a more specific sub-package to install.
Wrong:
go get github.com/garyburd/redigo
Correct:
go get github.com/garyburd/redigo/redis
答案5
得分: 3
如果你想获取该存储库中的所有包,可以使用...
来表示,例如:
go get code.google.com/p/go.text/...
英文:
If you want all packages in that repository, use ...
to signify that, like:
go get code.google.com/p/go.text/...
答案6
得分: 2
你可以尝试从 mod 下载软件包。
go get -v all
英文:
you can try to download packages from mod
go get -v all
答案7
得分: 1
我遇到了完全相同的错误代码,检查了我的代码库后发现没有go文件,只有更多的目录。所以对我来说,这更像是一个误导而不是一个错误。
我建议执行以下操作:
go env
确保一切都正常,检查您操作系统中的环境变量,并检查您的shell(如bash)是否通过.bash_profile或.bashrc文件对其进行了修改。祝好运。
英文:
I had this exact error code and after checking my repository discovered that there were no go files but actually just more directories. So it was more of a red herring than an error for me.
I would recommend doing
> go env
and making sure that everything is as it should be, check your environment variables in your OS and check to make sure your shell (bash or w/e ) isn't compromising it via something like a .bash_profile or .bashrc file. good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论