我应该如何在 Go 1.6 中使用供应商? [英] How should I use vendor in Go 1.6?

查看:20
本文介绍了我应该如何在 Go 1.6 中使用供应商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我阅读了这个答案:Go 1.6 中的供应商,然后我使用以我为例.

First I have read this answer: Vendoring in Go 1.6, then I use it as my example.

我的 gopath 是 GOPATH="/Users/thinkerou/xyz/",如下所示:

My gopath is GOPATH="/Users/thinkerou/xyz/", and the follow like:

thinkerou@MacBook-Pro-thinkerou:~/xyz/src/ou$ pwd
/Users/baidu/xyz/src/ou
thinkerou@MacBook-Pro-thinkerou:~/xyz/src/ou$ ls
main.go vendor

现在,我使用go get,然后变成这样:

Now, I use go get, then becomes this:

thinkerou@MacBook-Pro-thinkerou:~/xyz/src/ou$ ls
main.go vendor
thinkerou@MacBook-Pro-thinkerou:~/xyz/src/ou$ cd vendor/
thinkerou@MacBook-Pro-thinkerou:~/xyz/src/ou/vendor$ ls
vendor.json
thinkerou@MacBook-Pro-thinkerou:~/xyz/src/ou/vendor$ cd ../..
thinkerou@MacBook-Pro-thinkerou:~/xyz/src$ ls
github.com ou
thinkerou@MacBook-Pro-thinkerou:~/xyz/src$ cd github.com/
thinkerou@MacBook-Pro-thinkerou:~/xyz/src/github.com$ ls
zenazn

vendor.json 是这样的:

{
    "comment": "",
    "package": [
        {
            "path": "github.com/zenazn/goji"
        }
    ]
}

那我应该用什么命令呢?为什么没有使用vendor?我的go版本是1.6.2.

then, I should use what commands? why have no use vendor? My go version is 1.6.2.

推荐答案

在 Go1.6 中,vendoring 内置于您阅读时.这是什么意思?只需要记住一件事:

With Go1.6, vendoring is built in as you read. What does this mean? Only one thing to keep in mind:

当使用go工具如go buildgo run时,他们首先检查依赖是否位于./vendor/.如果是这样,请使用它.如果没有,请恢复到 $GOPATH/src/ 目录.

When using the go tools such as go build or go run, they first check to see if the dependencies are located in ./vendor/. If so, use it. If not, revert to the $GOPATH/src/ directory.

实际的查找路径"在 Go 1.6 中,按顺序是:

The actual "lookup paths" in Go 1.6 are, in order:

./vendor/github.com/zenazn/goji
$GOPATH/src/github.com/zenazn/goji
$GOROOT/src/github.com/zenazn/goji

话虽如此,go get 将继续安装到你身上 $GOPATH/src;并且,go install 将安装到 $GOPATH/bin 用于二进制文件或 $GOPATH/pkg 用于包缓存.

With that said, go get will continue to install into you $GOPATH/src; and, go install will install into $GOPATH/bin for binaries or $GOPATH/pkg for package caching.

呵呵,有了上面的知识,其实很简单:

Hehe, armed with the knowledge above, it's pretty simple:

mkdir -p $GOPATH/src/ou/vendor/github.com/zenazn/goji
cp -r $GOPATH/src/github.com/zenazn/goji/ $GOPATH/src/ou/vendor/github.com/zenazn/goji

简而言之,要使用vendoring,您需要使用相同的github.com/zenazn/goji 完整路径将文件复制到您的vendor Director 中.

In short, to use vendoring, you copy the files using the same github.com/zenazn/goji full path, into your vendor director.

现在,go build/install/run 工具将看到并使用您的供应商文件夹.

Now, the go build/install/run tooling will see and use your vendor folder.

与其查找和复制所有 25 个以上的供应商项目、管理它们的版本、更新其他项目等……不如使用依赖项管理工具.那里有很多,稍微谷歌一下就会指出你几个.

Instead of finding and copying all 25+ vendor items, managing their versions, updating other projects etc... It would be better to use a dependency management tool. There are many out there and a little googling will point to you several.

让我提两个与供应商文件夹一起使用并且不会与您对抗的:

Let me mention two that works with the vendor folder and doesn't fight you:

  • godep
  • 总督

简而言之,这些工具将检查您的ou 代码,找到远程依赖项,并您的$GOPATH/src你的 $GOPATH/src/ou/vendor 目录(实际上,无论你在运行它们时所在的当前目录).

In short, these tools will inspect your ou code, find the remote dependencies, and copy them from your $GOPATH/src to your $GOPATH/src/ou/vendor directory (actually, whatever current directory you are in when you run them).

例如,假设您使用常规 GOPATH/src/github 安装依赖项,在 $GOPATH/src/ou/ 项目中安装了所有依赖项并正常工作.您的项目运行并且您的测试验证一切都在您拥有的存储库的确切版本上正常工作.以 Godep 为例,您可以从项目根文件夹 $GOPATH/src/ou/ 运行它:

For example, say you have all of your dependencies installed and working normally in your $GOPATH/src/ou/ project using the normal GOPATH/src/github installation of your dependencies. Your project runs and your tests validate everything is working with the exact version of the repos you have. With Godep as an example, you'd run this from your project root folder $GOPATH/src/ou/:

godep save ./...

这会将您的项目使用的所有依赖项复制到您的 ./vendor 文件夹中.

This would copy all dependencies your project uses into your ./vendor folder.

Godep 是迄今为止最受欢迎的.他们在 Gopher Slack 小组中有自己的 Slack 频道.而且,这是我在我的团队中使用的.

Godep is by far and large the most popular. They have their own Slack channel on the Gopher Slack group. And, it's the one I use on my teams.

Govendor 是我读过的另一个替代方案,它有一个不错的同步功能.不过我没用过.

Govendor is another alternative I read has a nice sync feature. I haven't used it though.

这纯粹是个人意见,我相信仇恨者会反对...但由于我需要完成我关于这个主题的博客文章,让我在这里提一下,大多数人对 Go 中的依赖管理过于担心.

This is purely opinion, and I'm sure haters will downvote... But as I need to finish my blog post on the subject, let me mention here that most people worry too much about depdency management in Go.

是的,需要将存储库锁定到您依赖的版本,以便确保您的系统在生产环境中构建.是的,需要确保依赖项中断某事的方式不会发生重大变化.

Yes, there is a need to lock in a repo to a version you depend on so you can ensure your system builds in production. Yes there is a need to ensure no breaking changes to a way a dependency is interrupting something.

绝对使用依赖管理.

但是,在现实中,过度使用了锁定大量依赖项的简单项目......

But, there is overuse of simple projects that lock in huge amounts of dependencies when in reality...

您可能只需要锁定 1 个依赖项;否则,您需要最新版本的 MySQL 驱动程序和用于错误修复的测试断言框架.

You may only need to lock in only 1 dependencies; otherwise, you want the latest version of MySQL drivers and test assertion frameworks for bug fixes.

这是除了依赖管理工具之外使用 ./vendor/ 文件夹可以真正发挥作用的地方:您只需要复制需要锁定的存储库.

This is where using the ./vendor/ folder apart from dependency managrment tools can really shine: you'd only need to copy that repo that need you lock in.

您有选择地选择一个行为不端的回购并将其放入您的 ./vendor/文件夹中.通过这样做,您是在告诉您的消费者:

You selectively pick the one misbehaving repo and put it into your ./vendor/ folder. By doing this, you are telling your consumers:

嘿,这个 repo 需要在本次修订中保留.所有其他的都很好,使用最新的并且经常使用 go get -u ./... 更新;但是,这个在更新版本时失败了,所以不要升级这个 repo.

Hey, this one repo needs to be held back at this revision. All others are fine and use the latest of those and update often with go get -u ./...; but, this one failed with newer versions so don't upgrade this one repo.

但如果使用依赖管理工具全面保存所有依赖,您基本上是在告诉您的消费者:

But if blanketly saving all your dependencies with a dependency management tool, you are basically telling your consumers:

供应商文件夹中的 20 个存储库中的一个或多个存储库可能有问题,也可能没有问题.您可能会也可能无法更新它们.您可能会也可能无法获得最新的 MySQL 驱动程序.我们只是不知道哪些可能会或可能不会导致问题,只是锁定了在我运行 godep save 时起作用的东西.所以是的,升级风险自负.

There may or may not be a problem with one or more repos out of the 20 in the vendor folder. You may or may not be able to update them. You may or may not be able to get the latest MySQL driver. We simply don't know which may or may not be causing problems and just locked in something that worked at the time that I ran godep save. So yeah, upgrade at your own risk.

就我个人而言,我遇到过几次这种情况.一个依赖项更新了一个重大更改,我们有几十个依赖于它的存储库.在/vendor 中只提供一个 repo 允许我们使用那个版本的依赖项,而 go get ./... 继续正常运行所有其他 repos 以获得最新版本.我们使用 PSQL 和 MySQL 等最新的错误修复来运行(有针对这些的不断修复!)等等.

Personally, I have ran into this several times. A dependency was updated with a breaking change, and we have dozens of repos dependent on it. Vendoring just that one repo in /vendor allows us to use that one version of dependency, while go get ./... continues to run normally for all other repos to get the latest. We run with the latest bug fixes in PSQL and MySQL and others (there are constant fixes for these!) and so on.

这篇关于我应该如何在 Go 1.6 中使用供应商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆