Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Alex Edwards

Rating No ratings yet

Version 2023.2 Go is a great language for building web applications. But teaching yourself from blog posts and the standard library documentation can be a big time-sink and leave you with more questions than answers. You might be wondering things like: Where can I see a concrete example of a real-world web application? How is it best to structure and organize my code? How do I manage and version control dependencies? And how do I effectively test my web application? Let’s Go answers these questions for you — and a whole lot more — helping you get up to speed quickly and saving you months of research and experimentation. The book walks through the start-to-finish build of a complete working application, helping you to learn by doing. You’ll get practical experience implementing common real-life code patterns, the topics that you’re learning about are put in context, and you’ll end up with a well-structured codebase that you can adapt for your own future work. By the end of the book, you’ll have all the knowledge and confidence that you need to build your own professional, production ready, web applications.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A hands-on, start-to-finish guide to building a production-ready web application in Go, taking you from a bare "Hello" handler to a structured, tested, deployable codebase. Best for developers who know some Go basics and want one concrete, real-world project to learn professional patterns from. 【Book Arc】 - **Opening (~0%–10%)**: Sets up the project and the three essentials of any Go web app — handlers, a router (servemux), and a built-in web server — then gets a first working page running. - **Early (~10%–32%)**: Establishes professional structure and configuration: project layout (cmd/internal/ui), command-line flags, dependency injection into handlers, logging, and connecting to a MySQL database via a connection pool. - **Middle (~32%–55%)**: Builds the data layer and rendering pipeline — SQL models, transactions, template caching, a templateData holding struct, and helper methods for rendering and error handling. - **Late (~55%–80%)**: Adds the cross-cutting concerns of a real app: middleware (security headers, request logging, recovery), session management, user authentication and authorization, and form validation. - **Ending (~80%–100%)**: Hardens and ships — testing handlers and middleware, plus the operational concerns needed to run the app in production. (Excerpts thin out here; specifics beyond testing are not covered.) 【Key Takeaways】 - **Handlers, servemux, and the built-in server are the foundation** (Opening): Go needs no external server like Nginx, and understanding the `ServeHTTP` signature is the key that unlocks everything else. - **Project layout is a design decision, not decoration** (Early): The cmd/internal/ui split cleanly separates Go code from non-Go assets and reusable helpers, giving you a structure you can carry to future projects. - **Inject dependencies rather than reaching for globals** (Early): Passing loggers, DB pools, and template caches into handlers via an application struct keeps code testable and explicit. - **`sql.DB` is a connection pool, not a connection** (Early): It is long-lived, concurrency-safe, and should be initialized once in `main()` — never opened inside a short-lived handler. - **Transactions guarantee atomicity** (Middle): Always pair `tx.Commit()` with a deferred `tx.Rollback()` so connections return to the pool and multi-statement operations either fully succeed or leave the database untouched. - **Templates take exactly one data item** (Middle): Wrapping dynamic data in a `templateData` struct is the lightweight, type-safe way to render multiple values on one page. - **Middleware composes cross-cutting behavior** (Late): Wrapping handlers lets you apply security headers, logging, and recovery uniformly without polluting business logic. - **Testing is part of the build, not an afterthought** (Ending): The book treats handler and middleware tests as a first-class step toward production readiness. 【Reading Tips】 - **Type the code, don't just read it.** This is a build-along book; the value comes from running each iteration and seeing the app evolve. - **Deep-read the Early and Middle sections** on project structure, dependency injection, and the data layer — these are the patterns you'll reuse most. - **Skim setup/installation chapters** if your environment is already configured; the conceptual payoff is in the code, not the tooling steps. - **Pause at each refactor.** When the book extracts a helper or introduces a struct, note *why* — that reasoning is the transferable lesson. - **Keep the finished codebase as a template** for your own projects rather than treating it as a one-off exercise. 【Coverage Limits】 This guide is synthesized from stratified excerpts covering roughly the first half of the book in detail; the later chapters on authentication, testing, and deployment are represented only by their themes, so specific techniques there are not summarized.
Page 8
book is for general informational purposes only. While the author and publisher have made every effort to ensure the accuracy of the information within this...
View in text
Excerpt 2
er, navigation and metadata inside the <head> HTML element. To save us typing and prevent duplication, it’s a good idea to create a base (or master) template...
View in text
Excerpt 3
ing and closing connections to the database via the driver. The connection pool is safe for concurrent access, so you can use it from web application handler...
View in text
Excerpt 4
e earlier and return. ts, ok := app.templateCache[page] if !ok { err := fmt.Errorf("the template %s does not exist", page) app.serverError(w, err) return } /...
View in text
Excerpt 5
this point and the code should compile without any errors. Updating the HTML template The next thing that we need to do is update our create.tmpl template to...
View in text
Excerpt 6
und(w) } else { app.serverError(w, err) } return } data := app.newTemplateData(r) data.Snippet = snippet app.render(w, http.StatusOK, "view.tmpl", data) } Fe...
View in text
Excerpt 7
rent user’s session but retain any data associated with the session. It’s good practice to do this before login to mitigate the risk of a session fixation at...
View in text
Excerpt 8
seWriter, r *http.Request) { var form snippetCreateForm err := app.decodePostForm(r, &form) if err != nil { app.clientError(w, http.StatusBadRequest) return...
View in text
Tags
AI categories
GoBackendWeb Technology
Publisher: alexedwards.net
Publish Year: 2023
Language: English
Pages: 426
File Format: PDF
File Size: 7.5 MB
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…