Is go build supposed to download dependencies?

I apologise for a newbie question but my understanding was that go build is supposed to download the dependencies automatically, but for me it requests to download it using go get. Once I download the dependencies it builds happily.

I’m using Go version 1.21.3

go % go version
go version go1.21.3 darwin/arm64

Hi @Aaron_Stromas, welcome to the forum.

From my experience, I would say that only “go get”, “go mod tidy”, and “go mod download” can download a module from a remote repo.

The other tools, especially the compiler, do not interpret the module path as a repo URL.

Or that’s what I thought I had read somewhere. However, I have not found anything in the docs that would confirm my observations.

go install can also download modules. And I agree that go build is not supposed to download the dependencies automatically. I think go devs would consider that invasive; especially people who vendor dependencies to carefully control them.

@Aaron_Stromas, check out the docs for go build:

Thank you, @christophberger and @Dean_Davidson. I was reading the O’Reiley LearningGo by J. Bodner which seems to suggest that packages are downloaded automatically

$ go build
go: finding module for package github.com/learning-go-book/simpletax
go: finding module for package github.com/shopspring/decimal
go: downloading github.com/learning-go-book/simpletax v1.1.0
go: found github.com/learning-go-book/simpletax in
github.com/learning-go-book/simpletax v1.1.0
go: found github.com/shopspring/decimal in github.com/shopspring/decimal v1.2.0

but it was not happening for me, so I was wondering if something wrong was with my setup.
Thank you both.

Well, that IS interesting.

Apologies for the late reply. I suppose you have the first edition?

In the second edition (early release, publishing planned for Jan 24), the steps are fixed:

$ go get ./...
go: downloading github.com/learning-go-book-2e/simpletax v1.1.0
go: added github.com/learning-go-book-2e/simpletax v1.1.0
go: added github.com/shopspring/decimal v1.3.1
$ go build

First ed:
First edition

Second ed:
Second edition

True, but mainly as a side effect of building and installing a binary.