Lesson 31 of 60 · Go
Building and Publishing a Go Binary
Duration: 20 minutes
Building Executables
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
go build compiles a binary for the host OS/architecture.
$ go build -o myapp ./cmd/myapp
Cross‑compilation
# Build Linux binary on macOS
env GOOS=linux GOARCH=amd64 go build -o myapp-linux
Distributing binaries
- Upload to GitHub Releases.
- Use
goreleaserfor automated builds.
$ go install github.com/goreleaser/goreleaser/v2@latest
$ goreleaser release --snapshot --skip-publish
Containerizing with Docker
FROM golang:1.22-alpine AS builder
WORKDIR /src
COPY . .
RUN go build -o /app ./cmd/app
FROM alpine:latest
COPY --from=builder /app /app
ENTRYPOINT ["/app"]