Leanpub Header

Skip to main content

Building Production Web APIs With Go

Build RESTful APIs using PostgreSQL, sqlc, pgx, Docker, OpenAPI and Kubernetes

Building Production Web APIs With Go
This book is 100% completeLast updated on 2026-10-05

Build a Go Web API that goes beyond “Hello, World”

Getting a REST API running in Go is easy.

Building one that is structured well, handles concurrent requests correctly, manages database changes safely, can be tested, containerized, documented, and deployed is where things become interesting.

Building Production Web APIs with Go takes you through that journey step by step.

You begin with a tiny HTTP server and gradually develop it into a complete library API backed by PostgreSQL. Rather than presenting a finished architecture and asking you to copy it, the book introduces problems as the application grows, then shows how to solve them. You see not only what the final code looks like, but why the design changes are necessary.

Along the way, you will learn how to:

  • Build RESTful HTTP APIs using Go, net/http, and Chi
  • Work directly with PostgreSQL using pgx
  • Generate type-safe database code from SQL with sqlc
  • Manage schema changes with Goose migrations
  • Separate HTTP models from database models
  • Structure a Go backend for maintainability
  • Handle transactions, row locking, and concurrent requests safely
  • Enforce data integrity with PostgreSQL constraints
  • Write unit tests using database abstractions and test doubles
  • Understand what unit tests cannot prove without a real database
  • Document your API with OpenAPI and Swagger UI
  • Package the application with Docker and Docker Compose
  • Implement graceful shutdown
  • Deploy the complete application to Kubernetes

The example application is deliberately realistic. A seemingly simple operation—allowing a user to borrow a book—eventually exposes concurrency problems and leads naturally to transactions, PostgreSQL row-level locking, and database constraints. The emphasis throughout is on understanding the engineering decisions behind the code rather than simply copying recipes.

Who is this book for?

This book is for developers who already know the basics of Go and SQL and want to learn how the pieces of a real backend application fit together.

It is not a beginner’s introduction to the Go language, nor is it a comprehensive reference to PostgreSQL, Docker, or Kubernetes. Instead, it concentrates on the practical decisions involved in taking a small Go application toward a production-oriented architecture.

If you can write basic Go programs and understand simple SQL queries, this book will help you make the transition from “I can build an API” to “I understand how to engineer a backend.”

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

Getting a REST API running in Go is easy.

Building one that is well structured, handles concurrent requests correctly, manages database changes safely, and can be tested, containerized, documented, and deployed is where things get interesting.

Building Production Web APIs with Go takes you through that journey step by step.

WHAT YOU'LL BUILD

You start with a tiny HTTP server and grow it into a complete library API backed by PostgreSQL. Rather than presenting a finished architecture and asking you to copy it, the book introduces problems as the application grows and then shows how to solve them. You see not only what the final code looks like, but why each design change is necessary.

The example is deliberately realistic. A seemingly simple feature, letting a user borrow a book, eventually exposes a concurrency problem. Solving it leads naturally to transactions, PostgreSQL row-level locking, and database constraints.

WHAT YOU'LL LEARN

  • - Build RESTful HTTP APIs with Go, net/http, and Chi
  • - Work directly with PostgreSQL using pgx
  • - Generate type-safe database code from SQL with sqlc
  • - Manage schema changes with Goose migrations
  • - Separate HTTP models from database models
  • - Structure a Go backend for maintainability
  • - Handle transactions, row locking, and concurrent requests safely
  • - Enforce data integrity with PostgreSQL constraints
  • - Write unit tests using database abstractions and test doubles
  • - Understand what unit tests cannot prove without a real database
  • - Document your API with OpenAPI and Swagger UI
  • - Package the application with Docker and Docker Compose
  • - Implement graceful shutdown
  • - Deploy the complete application to Kubernetes

WHO IS THIS BOOK FOR?

Developers who already know the basics of Go and SQL and want to see how the pieces of a real backend application fit together. If you can write basic Go programs and understand simple SQL queries, this book will help you move from "I can build an API" to "I understand how to engineer a backend."

WHO IS IT NOT FOR?

This is not a beginner's introduction to the Go language, and it is not a comprehensive reference to PostgreSQL, Docker, or Kubernetes. It concentrates on the practical decisions involved in taking a small Go application toward a production-oriented architecture.

Share this book

Categories

Author

About the Author

Iede Snoek

Iede Snoek is a senior backend architect and software developer with more than 30 years of experience building production systems. He has worked extensively with C#, Python, Go, PostgreSQL, Azure, Docker, Kubernetes, and backend API architecture.

He writes about software development and architecture at CodeNomad.nl and develops open-source libraries for .NET and Rust. His particular interest is in the reasoning behind good software design: not simply how to make code work, but how to make systems maintainable, reliable, and understandable.

Iede has a Master's degree in Musicology from Utrecht University and also studied computer science subjects including parallel programming, formal methods, and architecture. Building Production Web APIs with Go combines his interest in teaching with three decades of practical backend development experience.

Contents

Table of Contents

Table of Contents

  • Preface
  • Acknowledgements
  • Building Your First Go Web API
    • Why Go for Web APIs
    • What Are We Going to Build in This Book?
    • Source Code
    • What Will This Book Cover?
    • What This Book Doesn’t Cover
    • Prerequisites
    • Creating the Project
    • Your First Go Web API
    • Understanding the Code
    • It Works
    • What We’ve Built
  • Adding PostgreSQL and sqlc
    • Choosing PostgreSQL
    • Installing PostgreSQL
    • Why sqlc Instead of an ORM
    • Installing sqlc and Goose
    • Creating the First Migration
    • Writing SQL Queries
    • Connecting to the Database
    • Summary
  • Creating the Endpoints
    • Creating the Routes
    • Trying It Out
    • Summary
  • Refactoring the API
    • Writing JSON
    • Writing Errors
    • Extracting the ID
    • Creating a BookResponse
    • Adding Validation
    • Simplifying main
    • Summary
  • Adding Users and Making Books Borrowable
    • Updating the Database and the Queries
    • The User Handlers
    • Updating the Main Method
    • Borrowing and Returning Books
      • The New Response Types
      • Adding Borrowing Functionality to the Book Handlers
    • Testing the New Functionality
      • Testing the User Functionality
      • Testing the Borrow and Return Functionality
      • Bonus: Deleting a User
    • Summary
  • Hardening the API
    • The Fix
      • Fixing the Null Values in the Response
      • Making the Borrowing Process Concurrency Safe
      • Updating main
    • Preserving History
      • Adding a Borrowing History Endpoint
      • The Duplicate Email Problem
    • Testing the API
    • Summary
  • Adding Unit Tests
    • Why Automated Tests Matter
    • Testing Without a Database
    • Creating a Test Double
    • Testing the Book Handlers
    • Testing the User Handlers
    • Testing Borrowing-Related Endpoints
    • What Unit Tests Cannot Prove
    • Running the Tests
    • Summary
  • Local Deployment
    • Prerequisites
    • How Does Docker Work?
      • Building the Image
      • Running Your App in Docker Compose
      • Testing Persistence
    • Bonus: Shrinking the Migration Image
    • Summary
  • API Documentation and Graceful Shutdown
    • Installing the swag Tool
    • Adding Swagger Annotations
    • Annotating the Handlers
      • The User Handlers
    • Generating the Swagger Docs
    • Adding Graceful Shutdown
    • Robust Error Handling During Shutdown
    • Summary
  • Deploying to Kubernetes
    • What Is Kubernetes?
    • Why Use Kubernetes?
    • Important Terms
    • Preparing the Go Code
    • Preparing the Images
    • Kubernetes Prerequisites
      • Kubernetes and Minikube Commands
    • Creating the Kubernetes Manifests
    • The Deployment Order
    • What We Have Built
    • Summary

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub