从 go.mod 文件中安装所有依赖项。

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

Installing all dependencies from a go.mod file

问题

在Go语言中,与npm install相当的命令是go mod download。这个命令会下载并安装go.mod文件中列出的所有依赖项。

英文:

What is the golang command that is equivalent to npm install

npm install downloads all the dependencies listed in the package.json file.

Having said that, what is the command that downloads all the dependencies in the go.mod file?

答案1

得分: 11

如果你只有一个 go.mod 文件,并且你使用的是 Go 1.16 或更新版本:

  • 如果你只想运行你的代码,可以使用 go buildgo run .,你的依赖项将会自动下载和构建
  • 如果你想在本地保存一份依赖项的副本,可以使用 go mod vendor

上述两种方法都会创建一个 go.sum 文件(这由 Go 工具维护 - 你可以忽略它,但要将其纳入版本控制)

vendor 命令会创建一个 vendor 文件夹,其中包含所有依赖项的源代码副本。**注意:**如果你使用 vendor 方法,当依赖项发生变化时,你需要运行 go mod vendor,以便将副本下载到 vendor 文件夹中。优点是你的代码可以在没有网络连接的情况下构建。缺点是你需要保持它的更新。

这些内容应该能帮助你开始日常使用。

如果你想了解有关模块的更多信息,可以参考这个链接。

英文:

If you have only a go.mod and you have Go 1.16 or later:

  • If you just want to run your code, use go build or go run . - your dependencies will be downloaded and built automatically
  • If you want to save a copy of your dependencies locally, use go mod vendor

Both the above will create a go.sum file (this is maintained by the Go Tools - you can ignore it, but do check it into version control)

The vendor command will create a vendor folder with a copy of all the source code from your dependencies. Note: If you do use the vendor approach, you will need to run go mod vendor if there is a change in your dependencies, so that a copy is downloaded to the vendor folder. The advantage is your code will build without an internet connection. The disadvantage is you need to keep it up to date.

That should get you started for every day use.

If you'd like to know all about modules, this is a good source.

答案2

得分: 2

现代化的Go模块:

go mod download

参考:https://go.dev/ref/mod#go-mod-download

通过将依赖项的下载与构建过程分离,我们可以利用Docker构建中的层缓存。这意味着如果只有一个小的代码更改,我们就不需要再次下载整个依赖项。

英文:

Morden Go Modules:

go mod download

Ref: https://go.dev/ref/mod#go-mod-download

By separating the download of dependencies from the build process, we can take advantage of layer caching in Docker build. This means that if there is just a small code change, we don't have to download the whole dependencies again.

huangapple
  • 本文由 发表于 2021年10月7日 10:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/69474609.html
匿名

发表评论

匿名网友

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

确定