Docker provides documentation on this exact topic here. The suggestion is to structure your Dockerfile like this:
FROM --platform=${BUILDPLATFORM} docker.io/golang:1.16.7-alpine AS build
ARG TARGETOS
ARG TARGETARCH
WORKDIR /src
ENV CGO_ENABLED=0
COPY go.* .
RUN go mod download
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/example .
FROM scratch
COPY --from=build /out/example /
A lot of that is boilerplate you can remove if you're only building for a single architecture; the parts related to caching are really only:
FROM docker.io/golang:1.16.7-alpine AS build
WORKDIR /src
COPY go.* .
RUN go mod download
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build go build -o /out/example .
FROM scratch
COPY --from=build /out/example /
This mounts a cache directory on /root/.cache/go-build, which is the default location for the go build cache. The first time you build your image it will populate this cache. Subsequent builds will re-use the cached files.
For this to work, you must build with DOCKER_BUILDKIT=1, i.e.:
DOCKER_BUILDKIT=1 docker build -t myimage .
Or use docker buildx:
docker buildx build -t myimage .
I have tested this out locally and it seems to work as intended (I have verified that in builds other than the first one, the go-build cache directory is populated prior to running go build).