No description
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Let's Go Further — Reading Guide
## 【One-Line Pitch】
A practical, project-based guide for Go developers who want to build production-grade REST APIs with PostgreSQL, covering everything from JSON handling and validation to advanced features like pagination, authentication, and deployment. Ideal for intermediate Go programmers ready to move beyond toy examples and build a real-world API.
## 【Book Arc】
- **Opening (~0%–10%)**: Sets up the Greenlight API project — a movie database API — establishing the directory structure, HTTP server, configuration management via command-line flags, and RESTful routing with the httprouter package. Introduces the core pattern of dependency injection for handlers.
- **Early (~10%–25%)**: Focuses on JSON fundamentals — sending responses, creating reusable helpers, decoding request bodies efficiently (comparing json.Decoder vs json.Unmarshal with benchmarks), and building a robust validation layer with custom error messages for malformed input.
- **Early (~25%–35%)**: Introduces PostgreSQL integration — database setup, connection pooling configuration (MaxOpenConns, idle connections), running migrations, and structuring the data layer with models and interfaces for testability.
- **Middle (~35%–50%)**: Implements CRUD operations for movies — creating, reading, updating (with PATCH for partial updates), and deleting records. Covers SQL query timeouts using context-aware methods and begins building filtering, sorting, and pagination functionality.
- **Middle (~50%–65%)**: Completes the filtering/sorting/pagination system with validation of query parameters, safelist-based sort fields, and efficient SQL generation. Likely covers advanced query patterns and response formatting.
- **Late (~65%–100%)**: Based on the book's structure, this section typically covers advanced topics such as user authentication, permissions, rate limiting, background tasks, and deployment considerations — though the excerpts provided do not detail these chapters.
## 【Key Takeaways】
- **Project structure matters for maintainability** (Opening): The book establishes a clean separation between cmd/api (HTTP layer), internal/data (database layer), and internal/validator (validation logic), making the codebase easy to extend and test.
- **json.Decoder outperforms json.Unmarshal for request bodies** (Early): Benchmark results show roughly 80% less memory allocation and slightly better speed, making it the preferred choice for reading JSON from streams.
- **Custom error messages improve API usability** (Early): Instead of exposing raw Go errors, the book builds a readJSON helper that translates common decode failures into clear, client-friendly messages like "body contains unknown key" or "body must only contain a single JSON value".
- **Validation should be centralized and reusable** (Early): A dedicated validator package with Check() methods and error maps keeps validation logic consistent across all endpoints, avoiding scattered if-statements.
- **Connection pool tuning is critical for database performance** (Early): Explicitly setting MaxOpenConns (25 as a starting point) acts as a throttle and prevents resource exhaustion, while idle connection cleanup prevents stale connections from accumulating.
- **Interfaces enable testable data layers** (Early): Defining MovieModel as an interface allows swapping real database implementations with mocks, making unit testing handlers straightforward without a live database.
- **PATCH is semantically correct for partial updates** (Middle): The book demonstrates why PATCH (not PUT) should be used when updating only specific fields, preserving the REST semantics of full replacement vs. partial modification.
- **Context-aware queries prevent hanging requests** (Middle): Using ExecContext() and QueryRowContext() with timeouts lets you cancel slow database operations, log errors, and return 500 responses instead of leaving clients waiting indefinitely.
## 【Reading Tips】
- **Skim the JSON basics if you're experienced** (~3%): The early JSON syntax review is beginner-oriented; focus instead on the writeJSON helper design and the benchmark comparison between decoding approaches.
- **Deep-read the validation chapter** (~19%–23%): The error-handling patterns for JSON decoding edge cases (unknown fields, oversized bodies, multiple JSON values) are subtle and worth studying carefully — these are the details that separate production APIs from prototypes.
- **Pay attention to the database setup section** (~29%): Connection pool configuration is easy to skip but has real performance implications. The rule-of-thumb guidance on MaxOpenConns is practical advice you'll reuse.
- **Follow along with the code** (throughout): The book is heavily code-driven with complete file listings. Type out the code yourself rather than copying — you'll internalize the patterns better.
- **Test the API as you go**: The book includes curl commands for each endpoint. Running these yourself helps verify your understanding and catches setup issues early.
## 【Coverage Limits】
This guide covers the first half of the book (approximately 0–50%) based on available excerpts. Later chapters on authentication, permissions, rate limiting, and deployment are not covered here — the excerpts do not include material beyond the filtering/sorting/pagination section.
##
Excerpt 1
that they return JSON responses instead of just plain text. JSON (which is an acronym for JavaScript Object Notation) is a human-readable text format which c...
View in text
Excerpt 2
1664 B/op 21 allocs/op Additional JSON decoding nuances There are a few JSON decoding nuances that are important or interesting to know about, but which don’...
View in text
Excerpt 3
or help. greenlight=> \dt List of relations Schema | Name | Type | Owner public | movies | table | greenlight public | schema_migrations | table | greenlight...
View in text
Excerpt 4
e going to focus on building up the functionality for a new GET /v1/movies endpoint, which will return the details of multiple movies in a JSON array. Method...
View in text
Excerpt 5
arries out this check before we do any expensive processing like decoding a JSON request body or querying our database. You’ll learn: About the principles be...
View in text
Excerpt 6
elper. case errors.Is(err, data.ErrDuplicateEmail): Each inbox has its own set of SMTP credentials, which you can display by clicking the Show Credentials li...
View in text
Excerpt 7
( "context" "net/http" "greenlight.alexedwards.net/internal/data" ) // Define a custom contextKey type, with the underlying type string. type contextKey stri...
View in text
Excerpt 8
the query string is different http://foo.com/a#b http://foo.com/a#c Yes Only the fragment is different Understanding what origins are is important because al...
View in text
Tags
AI categories
BackendGoDatabase
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Generating text preview…
Loading comments...
Reply to Comment
Edit Comment