如何安装依赖项的二进制文件?

huangapple go评论84阅读模式
英文:

How to install binaries of dependencies?

问题

我的Golang项目依赖于几个包(例如https://github.com/mattes/migrate和https://github.com/joho/godotenv)。我知道我可以分别使用go getgo install来安装它们,但是否有一种方法可以一次性安装项目的所有依赖项?理想情况下,我希望go install ./...migrategodotenv的可执行文件放在我的$GOPATH/bin目录中,但它只安装主包的可执行文件,而不是依赖项的可执行文件。

英文:

My Golang project depends on a few packages (i.e. https://github.com/mattes/migrate and https://github.com/joho/godotenv). I know I can go get or go install them separately, but is there a way to install all of the dependencies of a project in one shot? Ideally, I would expect go install ./... to put the migrate and godotenv binaries in my $GOPATH/bin, but it only installs the main package binary and not the binaries of the dependencies.

答案1

得分: 0

你是否正在寻找一种方法,可以从你的本地机器自动安装你想要调用的二进制文件,而不是通过代码中的go install ./...命令?

直接使用Go是不可能的,除非它们在同一个用户名或模式下列出,你可以在一个go install命令中输入。但是,上面列出的这两种选项足够不同,无法实现这一点。

选项1)告诉用户为每个依赖项调用go install(这是非常常见的)。

选项2)或者,在你的代码库中创建一个简单的install.sh命令的bash/batch文件来运行它们。它将为所有的依赖项调用go install。另一种更类似于UNIX/C的方法是创建一个Makefile,告诉用户运行make命令。

个人而言,我更喜欢选项2,因为它可以对你的服务或工具进行非常复杂的控制。你可以从简单开始,然后在将来添加测试、go generate命令或其他任何你喜欢的功能,而无需更改安装说明,只需运行make install即可处理。它还有助于持续集成服务器,你可以有一个make test或任何你想要的东西,但与普通用户通常运行的方式有所不同。所有这些都在一个可以稍后更改的Makefile中。

我认为我的更新脚本中有大约25个go get -u -v命令。

英文:

Are you asking for a way to install the binaries you want to call, from your local machine, automatically from a go install ./... of your code?

That isn't going to happen with straight up Go, unless they are listed in the same username or pattern you can type in a single go install command. But just the two listed above are different enough that that can't happen.

Option 1) Either state to your users to call go install for each Dependency (this is very common).

Option 2) Or, create a bash/batch file to run them with a simple install.sh command in your repo. Which, would call go install for all of your dependencies. Another more UNIX/C like approach would be to create a Makefile, and tell your users to run make

Personally, I prefer option 2 as it gives you very complex control over your service or utility. You can start simple; but, later on add tests, go generate commands and whatever else you like in the future - all without having to change your installation instructions just run 'make install' and it will handle it. It also helps with C.I. servers that you can have a make test or anything to your hearts desire; but, different enough that normal users would usually run it. All, within a single Makefile that you can change later.

I think my Updater script has like 25 go get -u -v commands.

huangapple
  • 本文由 发表于 2016年4月12日 08:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/36561638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定