Golang in production: logging, Docker, deploys, profiling
Optional — after you finish the basics. The difference between something that works and something you can put online and keep running.
- 01Error mapping and structured loggingOne place turns domain errors into HTTP statuses, and your logs become searchable.
- 02Rate limiting, CORS, and security headersStop somebody brute-forcing your login, and set the headers browsers rely on.
- 03Docker, Makefile, and running in a containerA 15MB image with no operating system in it, and the two runtime settings a container needs.
- 04Polish: docs, seeds, and the finishing touchesThe difference between "it works on my machine" and something another person can run.
- 05Harden it, and review your own codeRead what you wrote as if a stranger wrote it, hunting for eight things that commonly break.
- 06Profile it, ship it, and what comes nextMeasure before you optimise, push it, and pick the right next thing to build.
What each lesson covers
By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.
1. Error mapping and structured logging
- Build one error boundary that maps domain errors to HTTP statuses.
- Write structured logs with
log/slogthat include a request ID. - Explain the difference between liveness and readiness health checks.
Remember: The client gets a status and a safe message. You get the detail, the request id, and enough to find it in the logs.
2. Rate limiting, CORS, and security headers
- Build a per-IP rate limiter and apply the strictest limit to auth routes.
- Explain what CORS protects and configure an allowed-origin list.
- Set the standard security headers and an
http.Clientwith a timeout.
Remember: Rate limiting turns "an attacker gets unlimited guesses" into "an attacker gets twelve a minute". For a few dozen lines, few defences buy more.
3. Docker, Makefile, and running in a container
- Write a multi-stage Dockerfile that produces a small static Go image.
- Explain the two runtime settings a container needs (GOMEMLIMIT and GOMAXPROCS).
- Cross-compile for another OS and architecture with GOOS and GOARCH.
Remember: Go’s deployment story is one static binary. Do not undo it by shipping a full OS image or letting the runtime guess your container limits.
4. Polish: docs, seeds, and the finishing touches
- Version an API under
/v1from the first route. - Write a README that someone else can follow from clone to first request.
- Write doc comments that
go docshows correctly.
Remember: The project is finished when someone else can clone it and have it running in three commands.
- Version an API under
5. Harden it, and review your own code
- Run vet, staticcheck, the race detector and govulncheck before review.
- Find common anti-patterns in your own code with the self-review checklist.
- Attack your own API with bad input, missing auth and other users' IDs.
Remember: Reviewing your own code means reading it as if someone else wrote it and you have to maintain it. Be unkind to it now so production is kind to you later.
6. Profile it, ship it, and what comes next
- Capture a CPU or memory profile with pprof and find the hot spot.
- Write an honest benchmark with
b.Looporb.Nand compare results with benchstat. - Explain why you measure before optimising.
Remember: Correct, then clear, then fast, and only fast when a measurement told you to. An optimisation written without a profile often makes the code harder to read and no faster.