5

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?

Taz
  • 113
  • 1
  • 1
  • 5

2 Answers2

11

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.

Carl
  • 43,122
  • 10
  • 80
  • 104
2

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.

Jully
  • 21
  • 2