why we use go mod download before build go application?

When people make docker file of go application with module, most of them make like this.

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build

But when i tried like below, it also works well.

COPY . .
RUN go build

What’s the difference?
If i use go mod download command, can i get some advantages?

If we do RUN go mod download then later when we try to build the image again, Image build will be faster if there are no changes to go.mod/sum, because docker will cache the result of RUN go mod download and use it next time.

Here you are building your go application, dependencies will be fetched every time you build the image, which will result in slower image build.

I hope it helps :slight_smile:
Read more here.