Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
# The Power of Go Tools
## 【One-Line Pitch】
A practical, test-driven guide to building robust, reusable Go programs by mastering the language's tooling philosophy—perfect for intermediate Go developers who want to write production-quality code that's easy to test, maintain, and share.
## 【Book Arc】
- **Opening (~0%–10%)**: Establishes the core principle that `main` should be a thin wrapper—all substantive logic belongs in importable, testable packages. Builds a simple "hello" program to demonstrate how to structure code for testability.
- **Early (~10%–23%)**: Introduces functional options pattern for configuration, then builds a line/word counting CLI tool using test-driven development. Shows how to test command-line programs as actual executables using script tests, and demonstrates the payoff of reusable package design when adding new commands.
- **Early-Middle (~23%–39%)**: Covers file I/O testing with go-cmp for byte comparison, idempotent test design, and permission handling. Emphasizes the philosophy that well-designed applications need minimal logging—prefer stdout/stderr and metrics over verbose logs.
- **Middle (~39%–48%)**: Explores filesystem abstraction with `io/fs`, `fs.WalkDir`, and `MapFS` for fast, dependency-free testing. Includes benchmarking to compare real filesystem operations against in-memory alternatives.
- **Late (~48%–end)**: Demonstrates wrapping external commands (like macOS `pmset`) in Go packages, using regular expressions to parse command output, and testing integration with external dependencies through build tags and isolation techniques.
## 【Key Takeaways】
- **Keep `main` trivial** (Early): The `main` function should only call an entrypoint from your package—nothing more. This makes all substantive code testable and reusable by other programs, avoiding "dead end" code that can't be imported.
- **Functional options enable safe configuration** (Early): Use the `option func(*counter) error` pattern to validate settings at construction time. This guarantees fields are always valid without repeated checks, and prevents nil inputs from causing panics later.
- **Test CLIs as CLIs** (Early): Use script-based tests with `exec` and `stdout` assertions to verify your command-line tool behaves correctly as a real executable—no need to build binaries or write complex test plumbing.
- **Delegate commands to `Main...` functions** (Early): When adding new commands (like `MainWords` alongside `MainLines`), keep each delegate short and focused. This avoids flag-based switching and makes each command easy to understand and maintain independently.
- **Ask "what question does this log answer?"** (Early-Middle): Before adding logging, verify it serves a purpose. Most well-designed applications need minimal logging—send errors to stderr and use metrics (like Prometheus format) for aggregate information instead.
- **Use `MapFS` for fast filesystem tests** (Middle): Testing functions that take a filesystem is dramatically faster with in-memory `MapFS` than with real disk trees. Implied folders from pathnames work automatically, eliminating I/O overhead.
- **Prefer `fs.WalkDir` over recursive file walking** (Middle): The standard library's filesystem abstraction handles arbitrary-depth trees efficiently without stack memory concerns, and the error parameter gives you control over permission issues.
- **Wrap external commands when replication is impractical** (Late): When a system command (like `pmset` for battery status) requires complex system calls, wrapping it in a Go package with regex-based output parsing is simpler and equally portable.
## 【Reading Tips】
- **Deep-read the early chapters (0%–23%)**: The foundational patterns—thin `main`, functional options, script testing—are used throughout the rest of the book. Master these before moving on.
- **Skim the code listings, focus on the reasoning**: The book's value is in *why* designs are chosen (e.g., why `Main` returns an int exit status). Pay attention to the problem-solution narrative rather than typing out every example.
- **Watch for the "shameless duplication" moments**: The author deliberately shows imperfect solutions (like copying `MainLines` to create `MainWords`) to demonstrate refactoring opportunities. Note how the package design makes this duplication harmless.
- **Try the benchmarking section hands-on**: The `MapFS` vs. real filesystem benchmark is worth running yourself to internalize the performance difference and understand benchmark output format.
- **Skip ahead if you're not on macOS**: The `pmset` example is macOS-specific, but the principles (wrapping external commands, parsing output with regex) apply universally—substitute `acpi` or `powercfg` as needed.
## 【Coverage Limits】
Excerpts cover roughly the first half of the book (through ~48%), including testing philosophy, CLI construction, filesystem abstraction, and benchmarking. Later chapters on shells, HTTP client packages, and advanced integration testing are only partially represented in the source material.
##
Excerpt 1
om the example repo, if you like): package main import ( "github.com/bitfield/hello" ) func main() { hello.Print() } (Listing hello/2) This won’t work yet, o...
like: “If a filename is specified on the command line, the counter should count the lines in the specified file. Otherwise, it should count lines from standa...
utput file, and that it contains exactly the bytes we want. To do that, we can use os.ReadFile to open and read the file’s contents as got. Now, how should w...
tty fast! Can we do better using the MapFS-based filesystem? Let’s find out: func BenchmarkFilesInMemory(b *testing.B) { fsys := fstest.MapFS{ "file.go": {},...
on, though; why not? The answer is that real users wouldn’t normally want to enable dry-run mode, so they’d always have to supply a “mystery false”: shell.Ne...
r reading, in the same sort of way that we discussed in the chapter on arguments. You can use script to construct the sort of simple one-off pipelines that w...
Listing weather/4) Refactoring FormatURL is straightforward. We’re not adding any new behaviour, just moving paperwork around: func (c Client) FormatURL(loca...
"You can see here", "a battery, a key,", - " and", } (Listing game/4) How can we refactor our existing test to use the new testCase struct type? Well, let’s...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
The Power of Go Tools (John Arundel)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share to WeChat Moments
The Power of Go Tools (John Arundel)(Z-Library)
Scan the QR code with WeChat to open this book
Tap the ··· menu in the top-right corner
Choose Share to Moments
You are already in WeChat. Tap ··· in the top-right corner and choose Share to Moments.
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
The Power of Go Tools (John Arundel)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment