Web Development in Go
Web Development in Go
This part is a hands-on curriculum for Go web development: project structure, HTTP and routing, templates, databases, concurrency at the edge, sessions and auth, frontend/backend contracts, and testing—all explained in simple, direct language.
The real code and examples make the concepts easy to use. Coverage spans nearly every major aspect of Go web work, but keeps examples straightforward and avoids complex edge cases. That makes it ideal for learners and practitioners who want clarity and reliable progress.
You will learn to structure projects, design backend services, manage sessions, secure authentication, and integrate external APIs with step-by-step Bookstore examples. Techniques cover routing, concurrency, testing, logging, performance, and error handling—with code that is easy to understand and explanations that are easy to follow.
This edition focuses on modern Go features without niche scenarios, so lessons transfer to real projects. Simplicity over unnecessary detail takes you from setup toward deployment.
How this part relates to the rest of the book. Part
08-webcovers modern stacks (GOTH, gRPC, Huma, Postgres/Redis). Part13-network-systemsand deep dives go lower. This part is the practical spine: net/http first, clear modules, Bookstore REST + HTML, then secure auth and tests. Read it when you want a full web app path without framework overload.
Tooling baseline: Go 1.22+ routing (ServeMux method patterns) is the default. Gorilla Mux appears where method/path variables were historically taught; prefer stdlib mux for new work unless you need Mux-specific features. Use go mod init, go test -race, and structured logging (log/slog).
Learning outcomes
After this part you can:
- Scaffold a modular Go web project with clear package boundaries
- Serve HTTP, route requests, and return JSON or HTML consistently
- Render templates and static assets without mixing business logic into handlers
- Talk to a database through interfaces that stay testable
- Use concurrency for I/O-bound work without leaking goroutines
- Implement sessions, password hashing, and role-based access
- Define stable API contracts for frontend/backend interchange
- Mock dependencies and write focused automated tests
- Log, surface errors, and keep incident response simple
Key features
| Focus | What you practice |
|---|---|
| Project shape | Organised code, modular design, real workflows |
| REST APIs | net/http, optional Gorilla Mux, JSON habits |
| Security | Auth, roles, password hashing, secure cookies |
| Contracts | Clear API shapes, simple JSON interchange |
| Concurrency | Scale data fetching, avoid deadlocks |
| Testing | Mock DB/APIs/deps for fast, isolated tests |
| Sessions | Cookies, resilient login flows |
| Ops | Logging, tracing events, find problems quickly |
| Integration | HTTP client patterns for payments and third parties |
| Errors | Understandable messages, standard response plans |
Contents map
Setup: intro → structure → HTTP/routing
Content: templates → databases
Runtime: concurrency → sessions/auth
Boundary: frontend contracts → testing/debugging
Production: middleware → shutdown → rate limit → WS → uploads → capstone
| Chapter | File | Focus |
|---|---|---|
| 270 | this page | overview, path, Bookstore map |
| 271 | 271-intro-web-dev-go | why Go for web, request lifecycle, first server |
| 272 | 272-structuring-go-web-app | modules, packages, config, dependency wiring |
| 273 | 273-http-requests-routing | handlers, mux, middleware, JSON REST |
| 274 | 274-templating-rendering | html/template, layouts, static files |
| 275 | 275-databases | database/sql, repositories, Bookstore catalog |
| 276 | 276-concurrency-web | parallel fetches, timeouts, safe patterns |
| 277 | 277-sessions-auth-authz | cookies, sessions, passwords, roles |
| 278 | 278-frontend-backend-communication | API contracts, clients, external APIs |
| 279 | 279-testing-debugging | httptest, mocks, logging, performance checks |
| 280 | 280-middleware-patterns | chain, recover, request ID, access log |
| 281 | 281-graceful-shutdown-web | Shutdown, readiness drain, K8s |
| 282 | 282-rate-limiting | concurrency cap, token bucket |
| 283 | 283-websockets-basics | upgrade, heartbeats, hubs |
| 284 | 284-file-uploads-downloads | multipart, ServeContent, static |
| 285 | 285-project-bookstore-api-capstone | full Bookstore API project |
Suggested path
Week-shaped path (adjust pace):
Day 1 271 → 272 first server + layout
Day 2 273 routing + REST bookstore API
Day 3 274 → 275 HTML views + database
Day 4 276 → 277 concurrency + auth
Day 5 278 → 279 contracts + tests + logging
Day 6 280 → 282 middleware, shutdown, limits
Day 7 283 → 285 websockets, uploads, capstone
Running example: Bookstore
Throughout this part you build pieces of a small bookstore service:
Browser / SPA / curl
│
▼
┌─────────────┐
│ HTTP API │ /api/books, /api/auth/...
│ + HTML UI │ /books, /login
└──────┬──────┘
│
┌──────┴──────┐
│ domain + │ Book, User, Order
│ repository │
└──────┬──────┘
│
database / mocks
Keep each chapter’s snippets runnable with go mod init and a short main or _test.go. Prefer interfaces at boundaries so chapter 279 can mock cleanly.