如何在Go中处理全局外部工具?

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

How to deal with global external tools in Go?

问题

我来自Node环境,在那里我们有package.json来处理依赖关系。在Go中,我们有go.mod,这很好,但有些工具要求我们全局安装它们的二进制文件,而不是将其设置为我的项目的依赖,比如golangci-lint。例如,在Node中,当我们要求安装或依赖项时,eslint会自动安装。

让我困扰的是,当有人克隆我的项目或者我在CI/CD环境中时,他们必须手动安装这些依赖项。

处理类似golangci-lint这样的工具的最佳实践是什么?我做错了什么吗?

英文:

I come from Node environment, where we have package.json to deal with dependencies. In Go we have go.mod, which is great, but some tools ask us to install their binaries globally instead of setting as a dependency of my project, such as golangci-lint. In Node, for example, we have eslint, which is installed automatically for us when we ask to install or dependencies.

What bothers me is when someone clones my project or I'm in a CI/CD environment, they have to manually install those dependencies.

What's the best practice to deal with tools like golangci-lint? Am I doing something wrong?

答案1

得分: 1

Go语言不规定如何在任何目标平台上安装任意可执行文件,因为即使在同一个操作系统上(考虑到许多不同的Linux发行版和它们各自的软件包管理器),有很多不同的可能方法。

因此,由作者来建议用户如何最好地安装他们的软件。

例如,你提到的golangci-lint实用程序提供了自己的安装说明。还要注意,在“从源代码安装”部分的“为什么?”项目中,解释了一些通用解决方案(如go getgo install)中的一些挑战。

英文:

Go doesn't prescribe how to install arbitrary executables on any target platform because there are so many different possible ways to do it, even on the same operating system (consider the many different Linux distributions and their various package managers).

As such, it's up to authors to advise users how to best install their software.

For example, the golangci-lint utility you mentioned provides its own installation instructions. Note also that the "Why?" items in the "Install from Source" section explain some of the challenges in a one-size-fits-all solution such as go get or go install.

答案2

得分: 1

对于外部依赖,你可以创建适当的Makefile目标和工具,以及包含安装步骤等信息的Readme.md文件。

或者,如果你想使用Docker包含所有外部工具,可以构建docker compose文件并将其添加到代码库中。

对于内部依赖管理,请使用以下步骤:

  1. 运行go mod tidy命令。

  2. 运行go mod vendor命令。

这将在项目中创建一个vendor目录,并将所有下载的包保存在其中。

确保在更改某些依赖项或开始使用新的包时运行这些命令。

英文:

For external dependencies you could make proper Makefile targets and tools with how to Readme.md file with steps to install etc.

or build docker compose files to the repository if you want to include all external tools using Docker.

for internal dependancy management use:

1.go mod tidy

2.go mod vendor

this creates a vendor directory in the projects and keep all packages downloaded inside the vendor directory.

make sure to run these commands whenever you change some dependencies or start using new packages.

huangapple
  • 本文由 发表于 2022年7月22日 22:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/73082320.html
匿名

发表评论

匿名网友

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

确定