英文:
Go, how to import package from github and build without go get
问题
我需要从GitHub导入外部包“github.com/xy/packageX”到“MyProject/src/myProject.go”。所以我在myProject.go
中添加了import "github.com/xy/packageX"
。如果我运行go build
,我会得到以下错误:
myProject.go:9:2: import "github.com/xy/packageX": cannot find package
所以我必须运行go get
,然后再运行go build
。
有没有一种方法可以在不使用go get
的情况下构建它?或者我应该将它下载到MyProject/pkg
并将此链接添加到myProject.go
中?我使用的是Xubuntu 14.04。
英文:
I need to import external package from github “github.com/xy/packageX
” to MyProject/src/myProjcet.go
. So I added import “github.com/xy/packageX
” to myProject.go
. If I run go build, I get:
myProject.go:9:2: import “github.com/xy/packageX”: cannot find package
So I have to run go get
, and then go build
.
Is there a way, how it can be built without using of go get
? Or should I download it to MyProject/pkg
and added this link to myProject.go
? I am on Xubuntu 14.04.
答案1
得分: 3
如果您不想将您的Go项目依赖项添加到全局GOPATH中,您可以使用vendor机制。推荐使用Go 1.6+版本,具体请参考"Vendor Directories"。
进入使用该导入的包,并将其作为子模块添加到vendor子文件夹中。
cd GOPATH/src/myproject/mypackage
git submodule add -- https://github.com/<user>/<repo> vendor/github.com/<user>/<repo>
cd vendor/github.com/<user>/<repo>
go install
cd ../../../..
go install
注意:该仓库可能还有其他依赖项,您需要以类似的方式将它们添加到相同的vendor文件夹中。
英文:
If you don't want to add your go project dependency to the global GOPATH, you can vendor it. (go 1.6+ recommended: see "Vendor Directories")
Go to the package which uses that import, and add it as a submodule in a vendor sub-folder.
cd GOPATH/src/myproject/mypackage
git submodule add -- https://github.com/<user>/<repo> vendor/github.com/<user>/<repo>
cd vendor/github.com/<user>/<repo>
go install
cd ../../../..
go install
Note: that repo might have itself other dependencies, that you would need to add in a similar fashion (in the same vendor folder)
答案2
得分: 0
如果你的项目已经上传到代码仓库,比如GitHub,
当你初次执行"go install"时,应该改为"go get",甚至可以作为packageX。
英文:
If your project is up to the repository, such as github,
When the initial "go install" will be "go get" even as packageX.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论