Dev Skills
Go·Section 13 of 15 · 6 lessons · about 5 h 30 min

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.

  1. 01Error mapping and structured logging
    One place turns domain errors into HTTP statuses, and your logs become searchable.
  2. 02Rate limiting, CORS, and security headers
    Stop somebody brute-forcing your login, and set the headers browsers rely on.
  3. 03Docker, Makefile, and running in a container
    A 15MB image with no operating system in it, and the two runtime settings a container needs.
  4. 04Polish: docs, seeds, and the finishing touches
    The difference between "it works on my machine" and something another person can run.
  5. 05Harden it, and review your own code
    Read what you wrote as if a stranger wrote it, hunting for eight things that commonly break.
  6. 06Profile it, ship it, and what comes next
    Measure 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. 1. Error mapping and structured logging

    • Build one error boundary that maps domain errors to HTTP statuses.
    • Write structured logs with log/slog that 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. 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.Client with 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. 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. 4. Polish: docs, seeds, and the finishing touches

    • Version an API under /v1 from the first route.
    • Write a README that someone else can follow from clone to first request.
    • Write doc comments that go doc shows correctly.

    Remember: The project is finished when someone else can clone it and have it running in three commands.

  5. 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. 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.Loop or b.N and 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.

Test yourself