12

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?

2

2 Answers 2

21

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Modern 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.