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

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

问题描述

首先,我已经阅读了这个答案:在Go 1.6中开发,然后我使用它作为我的例子。



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

  thinkerou @ MacBook-Pro-thinkerou:〜/ xyz / src / ou $ pwd 
/ Users / baidu / xyz / src / ou
thinkerou @ MacBook-Pro-thinkerou:〜/ xyz / src / ou $ ls
main.go供应商

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

  thinkerou @ MacBook-Pro-thinkerou:〜/ xyz / src / ou $ ls 
main.go供应商
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:,
包:[
{
path:github.com/zenazn/goji
}
]
}

那么,我应该使用哪些命令?为什么没有使用 vendor ?我的go版本是1.6.2。

解决方案

使用Go1.6时,vendoring在您阅读时内置。这是什么意思?只有一件事要牢记:


使用 go 工具时 go build go run ,他们首先检查依赖关系是否位于中。 /供应商/ 。如果是这样,请使用它。如果没有,请还原为 $ GOPATH / src / 目录。 在Go 1.6中,实际的查找路径依次为:

  ./ 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



那么,我该如何使用./vendor?!?!


$ b $

  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 完整路径导入您的供应商总监。



现在,构建/安装/运行工具将会看到并使用您的供应商文件夹。 / p>

一种简单的方法,而不是手动复制所有内容



不是查找和复制所有25个供应商商品,而是管理他们版本,更新其他项目等......使用依赖关系管理工具会更好。



让我提一下与供应商文件夹一起使用的两个文件夹,它不会与您打交道:




  • godep

  • govendor



简而言之,这些工具将检查你的 ou 代码,找到远程依赖关系,并将它们从你的 $ GOPATH / src 您的 $ GOPATH / src / ou / vendor 目录(实际上,无论当前目录例如,假设你已经安装了所有的依赖项,并且在你的 $ GOPATH / src中正常工作/ ou / 项目使用正常的GOPATH / src / github安装您的依赖项。您的项目将运行,并且您的测试将验证所有内容与您拥有的回购站的确切版本一起工作。以Godep为例,您可以从您的项目根文件夹 $ GOPATH / src / ou / 运行:

  godep save。/ ... 

所有您的项目使用的依赖关系到你的./vendor文件夹中。



Godep是最受欢迎的。他们在Gopher Slack组拥有自己的Slack频道。而且,这是我在我的团队中使用的一个。



Govendor是我阅读的另一个备选方案,具有很好的同步功能。我还没有使用它。

在依赖管理工具的使用上



这纯属意见,我敢肯定仇恨者会陷入低谷......但是,因为我需要完成关于这个主题的博客文章,所以让我在这里提到,大多数人都非常担心Go中的偏见管理问题。

是的,需要将回购库锁定到您所依赖的版本,以便确保您的系统能够在生产环境中生成。是的,有必要确保不会中断某些依赖项的操作。



绝对使用依赖管理。



但是,过度使用简单的项目锁定了巨大实际上依赖关系的数量......


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


这是使用<$ c $除了依赖管理工具,c> ./ vendor / 文件夹可以真正发挥作用:只需复制需要锁定的repo。



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


嘿,这个回购需要在这次修订中被阻止。所有其他人都可以,并使用最新的那些,并经常更新 go get -u ./...;但是,这一个更新版本失败,所以不要升级这个回购。


但是如果用依赖性管理工具,你基本上是告诉你的消费者:


可能有或没有问题,供应商文件夹。您可能会或可能无法更新它们。您可能会也可能不会获得最新的MySQL驱动程序。我们根本不知道哪些可能导致问题,也可能不会导致问题,并锁定在运行 godep save 时运行的某些内容。所以是的,升级需要您自担风险。


就我个人而言,我碰到过好几次。依赖关系随着突变而更新,我们有数十个依赖于它的repos。在供应商处只提供一个回购协议允许我们使用该版本的依赖关系,而 go ./...继续正常运行以获得所有其他回购最新。我们运行PSQL和MySQL等最新的错误修复程序(这些修复程序经常修复!)等等。


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

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

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 is this:

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

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

解决方案

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

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.

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

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.

So, how do I use ./vendor?!?!

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

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

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

An easier way instead of copying everything manually

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
  • govendor

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).

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 ./...

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

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 is another alternative I read has a nice sync feature. I haven't used it though.

Over Usage of Dependency Management Tool

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.

Use dependency management for those, absolutely.

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

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.

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.

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

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:

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.

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天全站免登陆