Compilers and build caches do not belong in production images. Multi-stage builds solve this.
FROM golang:1.23 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /app ./cmd/app
FROM gcr.io/distroless/static
COPY --from=build /app /app
ENTRYPOINT ["/app"]
The final image only contains the static binary, usually a few megabytes instead of nearly a gigabyte.