Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Aaron Torres

Rating No ratings yet

Bridge the gap between basic understanding of Go and use of its advanced features Go (a.k.a. Golang) is a statically-typed programming language first developed at Google. It is derived from C with additional features such as garbage collection, type safety, dynamic-typing capabilities, additional built-in types, and a large standard library. This book takes off where basic tutorials on the language leave off. You can immediately put into practice some of the more advanced concepts and libraries offered by the language while avoiding some of the common mistakes for new Go developers. The book covers basic type and error handling. It explores applications that interact with users, such as websites, command-line tools, or via the file system. It demonstrates how to handle advanced topics such as parallelism, distributed systems, and performance tuning. Lastly, it finishes with reactive and serverless programming in Go.

AI Reading Assistant

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

AI guide
# Go Cookbook: A Practical Guide to Advanced Go Programming ## 【One-Line Pitch】 A hands-on recipe collection that bridges the gap between Go basics and advanced production techniques, perfect for developers who know the syntax but want to master real-world patterns in I/O, error handling, concurrency, and web services. ## 【Book Arc】 - **Opening (~0%–13%)**: Foundations of I/O and file systems, covering the common I/O interfaces, bytes buffers, CSV parsing, and template engines—establishing the "recipe" format of problem → solution → explanation. - **Early (~13%–23%)**: Command-line tool building with flags, environment variables, and configuration management across JSON, TOML, and YAML formats, plus Unix pipe handling. - **Early (~23%–32%)**: Data conversion and composition techniques—type assertions, interface handling, string/number conversions, and null-value encoding patterns for JSON. - **Middle (~32%–48%)**: Error handling deep-dive covering error creation patterns, wrapping/annotating errors, logging strategies, panic recovery, and structured logging with context. - **Middle (~48%–65%)**: Database and storage integration using database/sql with MySQL, connection pooling, rate limiting, and transaction management. - **Late (~65%–100%)**: Web services, middleware, reverse proxies, gRPC-to-JSON export, testing strategies (mocking, table-driven tests, fuzzing), and advanced topics like parallelism, distributed systems, performance tuning, reactive and serverless programming. ## 【Key Takeaways】 - **I/O interfaces are the universal currency of Go data handling** (Opening): bytes.Buffer satisfies io.Reader, enabling seamless integration with ioutil functions; choose buffers for streaming, slices for small datasets. - **Templates scale from CSV to full HTML rendering** (Early): Go's text/template and HTML/template packages support nesting, function imports, and iteration—start simple, grow into website rendering. - **Configuration formats are interchangeable with struct tags** (Early): JSON works out of the box; TOML and YAML via third-party libraries; struct tags provide hints that make multi-format support trivial. - **Error wrapping preserves context without losing the original** (Middle): Annotate errors as they propagate through layers; this pattern becomes critical in larger applications for debugging. - **Panic recovery requires deliberate design** (Middle): Use defer/recover patterns to catch panics at boundaries, but never as a substitute for proper error handling. - **Database/sql abstracts away driver differences** (Middle): The blank import pattern (`_ "github.com/go-sql-driver/mysql"`) connects drivers; connection pooling and transactions work uniformly across MySQL, Postgres, SQLite. - **Testing is a first-class citizen, not an afterthought** (Late): Table-driven tests improve coverage systematically; mocking via standard library or Mockgen enables isolated unit tests; fuzzing catches edge cases. ## 【Reading Tips】 - **Skim the "Getting ready" sections** if you're comfortable with Go module setup—they repeat environment configuration across chapters. - **Deep-read the "How it works" sections**—they contain the conceptual gold, explaining why patterns matter beyond just showing code. - **Focus on Chapter 4 (Error Handling)** as a cornerstone—it's referenced from earlier chapters and underpins robust application design. - **Treat each recipe as a template**—the code is meant to be adapted, not copied; the book's GitHub repo has complete, runnable examples. - **Skip around by project need**—this is a cookbook, not a novel; jump to the chapter matching your current problem. ## 【Coverage Limits】 This guide covers the opening through middle sections in detail (I/O, CLI tools, data conversion, error handling, databases). The later chapters on web services, testing, concurrency, and serverless programming are only briefly outlined based on the table of contents and early excerpts. ##
Page 14
8 Getting ready 208 How to do it... 208 How it works... 214 Chapter 8: Testing 215 Introduction 215 Mocking using the standard library 216 Getting ready 216...
View in text
Excerpt 2
his directory. 3. Copy tests from h t t p s ://g i t h u b . c o m /a g t o r r e /g o - c o o k b o o k /t r e e /m a s t e r /c h a p t e r 1/t e m p l a t...
View in text
Excerpt 3
if result[0] == '-' { result = result[1:] negative = true } // left pad with 0 if we're passed in value < 100 for len(result) < 3 { result = "0" + result } l...
View in text
Excerpt 4
text.Context, fields log.Fielder) context.Context { f := getFields(ctx) for key, val := range fields.Fields() { (*f)[key] = val } ctx = context.WithValue(ctx...
View in text
Excerpt 5
g, storing, and passing http.Client structs recipe. [ 155 ] Web Clients and APIs This recipe also explores using a case statement to switch on multiple chann...
View in text
Excerpt 6
:= ctx.Value(ID).(string); ok { return val } return "" } 5. Create a file called handler.go with the following contents: package middleware import ( "net/htt...
View in text
Excerpt 7
"https://golang.org", "https://godoc.org", "https://www.google.com/search?q=golang", } resps, err := waitgroup.Crawl(sites) if err != nil { panic(err) } fmt....
View in text
Excerpt 8
).Service(service, tag, false, nil) } 5. Create a file called exec.go with the following content: package discovery import ( "fmt" consul "github.com/hashico...
View in text
Tags
AI categories
GoProgramming LanguageBackend
ISBN: 1783286830
Publisher: Packt Publishing
Publish Year: 2017
Language: English
Pages: 392
File Format: PDF
File Size: 1.9 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…