# How Mat writes HTTP services in Go Page: https://stenobird.com/podcast/go-time-golang-software-engineering/how-mat-writes-http-services-in-go Text version: https://stenobird.com/podcast/go-time-golang-software-engineering/how-mat-writes-http-services-in-go.md Podcast: [Go Time: Golang, Software Engineering](https://stenobird.com/podcast/go-time-golang-software-engineering) Published: 2024-07-09T16:00:00+00:00 Episode link: https://changelog.com/gotime/322 Audio file: https://op3.dev/e/https://cdn.changelog.com/uploads/gotime/322/go-time-322.mp3 Processing state: processed JSON: https://stenobird.com/v1/public/podcasts/go-time-golang-software-engineering/episodes/how-mat-writes-http-services-in-go Duration seconds: 4267 ## Resource Mat Ryer shares architectural lessons from 13 years of building HTTP services in Go. The discussion focuses on moving away from complex abstractions toward simpler, more testable, and readable code patterns. ## Highlights - Main idea: Prioritize the standard library's http.Handler interface over custom function types to improve compatibility with third-party libraries - Practical takeaway: Use a top-level context in a 'run' function to manage the lifecycle of goroutines and enable programmatic graceful shutdowns - Failure mode: Avoid injecting environment variables or global state directly into handlers; instead, pass dependencies through structs to ensure deterministic testing - Practical takeaway: Optimize code for readability and simplicity, as code is read far more often than it is written - Main idea: Treat the package as the primary unit of testing to maintain clear boundaries and service integrity ## Topics Go programming, HTTP services, Software architecture, Unit testing, Dependency injection, Concurrency, Context pattern, Backend engineering ## Chapters - 6:35 — Moving from custom types to http.Handler: Why Mat transitioned from using custom handler function types to the standard library's http.Handler to reduce glue code and improve interoperability. - 17:10 — Managing dependencies and scale: How to structure dependencies like loggers and databases within structs to maintain clean APIs even as route counts grow into the hundreds. - 27:45 — Deterministic testing with context: Using a main application context to manage goroutine lifecycles and enable parallel, self-contained tests without side effects. - 33:25 — Graceful shutdowns and signal trapping: The importance of trapping interrupts and using context to ensure all background processes finish before the server exits. - 44:25 — Error wrapping and package boundaries: A discussion on the risks of leaking HTTP-specific error details into lower-level packages and how to handle error wrapping effectively. - 1:00:25 — The philosophy of readable code: Why the most 'boring' and simple code is often the best for long-term maintenance and team productivity. ## Actions - request_transcript: `POST https://stenobird.com/v1/public/podcasts/go-time-golang-software-engineering/episodes/how-mat-writes-http-services-in-go/transcription-requests` — Idempotently request low-priority transcript generation for this episode. - read_markdown: `GET https://stenobird.com/podcast/go-time-golang-software-engineering/how-mat-writes-http-services-in-go.md` — Read the agent-friendly Markdown representation of this episode resource. A page view does not enqueue transcription. Agents should invoke `request_transcript` explicitly when they need this episode processed. ## Transcript Full transcripts are not published on public pages unless there is a clear rights basis.