Go mod和go get的良好实践

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

Good practice for go mod and go get

问题

作为你的中文翻译,以下是翻译好的内容:

作为一个新的Go开发者,我想知道在使用go mod和go get时的最佳实践是什么。
我有一个go.mod文件,只包含与运行生产构建相关的模块。
在流水线过程中,我想运行一些代码质量检查,比如go vet/lint和其他测试。

为了运行这些检查,我需要获取一些额外的模块。但是一旦我这样做,我的go.mod文件就会被更新,但vendor/modules.txt文件却没有更新,这会导致一些问题。

我看到有两种解决方案,但都似乎不正确:

1-我在go get之后运行go mod vendor,这将更新我需要的所有文件。这并不特别麻烦,但我觉得需要调用两个不同的命令来更新我的模块有些奇怪。

2-我默认将用于linting/tests和其他检查的模块放在我的go.mod文件中。但是这对我来说听起来很奇怪,生产构建不需要linting模块,我不应该将它们发布出去。

我是不是想得太多了,应该选择第二种方法?还是还有其他的最佳实践?

希望对你有帮助!

英文:

Still new dev in Go here.
I am wondering what is the best practice in the usage of go mod and go get.
I have go.mod file, containing only modules related to run a production build.
During the pipeline, I do want to run some code quality check, such as go vet/lint and other tests.

To run this, I need to get get some additional modules. But once I do this, my go.mod gets updated, but not vendor/modules.txt, which create some issues.

I see 2 solutions to this, but both seem incorrect:

1-I run a go mod vendor after the go get, this will update all files I need. Not particularly annoying, but it seems weird to me to have to call two different command in order to update my modules.

2-I put by default the modules needed for the linting/tests and other check in my go.mod file. But that sounds weird to me, production build does not need the linting modules, I should not have to publish them.

Am I overthinking this, and I should just go with 2? Or is there an other good practice here?

答案1

得分: 2

是的,你应该这样做。你的go.mod文件包含了与你的项目一起工作所需的所有内容,而不仅仅是生产构建所需的内容。在其中包含非生产依赖项并不会造成任何问题。并不会将生产二进制文件中未使用的依赖项包含在内。

英文:

> I put by default the modules needed for the linting/tests and other check in my go.mod file. But that sounds weird to me, production build does not need the linting modules, I should not have to publish them.

Yes, you should do this. Your go.mod contains everything needed to work with your project, not just everything needed for a production build. There is no harm in including non-production dependencies there. Dependencies that are not used by your production binary are simply not included in the binary.

huangapple
  • 本文由 发表于 2021年10月5日 04:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/69442121.html
匿名

发表评论

匿名网友

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

确定