liveBook · Manning

What you will learn in Go by Example

Programmer's guide to idiomatic and testable code

by Inanc Gumus

Chapter by chapter

Chapter 1: Getting started

1.1 Why should you read this book? · 1.1.1 Learning by example · 1.1.2 Crafting idiomatic code · 1.1.3 Crafting testable code · 1.2 Why Go? · 1.3 Hello, gophers! · 1.3.1 Statically and strongly typed language · 1.3.2 Compilation and static binary · 1.3.3 Go runtime · 1.4 Concurrency · 1.4.1 Goroutines · 1.4.2 Go scheduler · 1.4.3 Channels · 1.5 Type system · 1.5.1 Composition instead of inheritance · 1.5.2 Implicit interfaces · 1.5.3 Testing with implicit interfaces · 1.6 Standard library · 1.7 Tooling · 1.8 Outro · 1.9 Source code and Go module · 1.10 Summary

What you learn in chapter 1 →

Chapter 2: Idioms and testing

2.1 Groundwork · 2.1.1 Overview · 2.1.2 Implementation · 2.2 Idioms · 2.2.1 Names · 2.2.2 Errors · 2.2.3 Fields · 2.2.4 Standard interfaces · 2.3 Testing · 2.3.1 Writing tests · 2.3.2 Running tests · 2.3.3 Writing a failing test · 2.3.4 Writing descriptive test failure messages · 2.3.5 Fixing the code · 2.4 Table-driven tests · 2.4.1 Creating a table of test cases · 2.4.2 Writing a table-driven test · 2.4.3 Running a specific test · 2.4.4 Identifying problems with table-driven tests · 2.5 Subtests · 2.5.1 Understanding subtests · 2.5.2 Writing a table-driven test with subtests · 2.5.3 Understanding the role of specific T pointers · 2.5.4 Running subtests · 2.5.5 Fixing the code · 2.6 Example tests · 2.6.1 Writing an example test · 2.6.2 Running example tests · 2.7 Other testing tools · 2.8 Summary

What you learn in chapter 2 →

Chapter 3: Test coverage and optimization

3.1 Test coverage · 3.1.1 Measuring test coverage · 3.1.2 Perfecting test coverage · 3.1.3 100% test coverage != bug-free code · 3.2 Benchmarking and optimization · 3.2.1 Writing and running benchmarks · 3.2.2 Using sub-benchmarks · 3.2.3 Profiling: Chasing out memory allocations · 3.2.4 Optimizing code · 3.2.5 Comparing benchmarks · 3.3 Compiler optimizations · 3.3.1 Inlining and dead-code elimination · 3.3.2 The sink variable · 3.3.3 A bright new future · 3.4 Parallel testing · 3.4.1 Running tests in parallel · 3.4.2 Running subtests in parallel · 3.4.3 Detecting data races · 3.5 Exercises · 3.6 Summary

What you learn in chapter 3 →

Chapter 4: Command-line interfaces

4.1 Groundwork · 4.1.1 Implementing the first version · 4.1.2 Running the first version · 4.2 Flag parsing · 4.2.1 Overview · 4.2.2 Higher-order value parsers · 4.2.3 Implementing a flag parser · 4.2.4 Integration and setting sensible defaults · 4.3 The flag package · 4.3.1 Integration · 4.3.2 Demonstration · 4.4 Value parsers · 4.4.1 Value interface · 4.4.2 Satisfying the Value interface · 4.4.3 Using Var · 4.5 Positional arguments · 4.5.1 Flags vs. positional arguments · 4.5.2 Customizing usage messages · 4.5.3 Setting a positional argument · 4.6 Validation · 4.6.1 Writing a custom validator · 4.6.2 Validating flags with a custom validator · 4.7 Exercises · 4.8 Summary

What you learn in chapter 4 →

Chapter 5: Dependency injection

5.1 Challenges to testability · 5.2 Testable programs · 5.2.1 Overview · 5.2.2 A place to store dependencies · 5.2.3 io.Writer’s role · 5.3 Decoupling · 5.3.1 Decoupling from the environment · 5.3.2 Decoupling the parser’s output · 5.3.3 Preparing for the HIT client · 5.3.4 Demonstration · 5.4 CLI tests · 5.4.1 Observing with a strings.Builder · 5.4.2 Streamlining tests · 5.4.3 Adding a helper · 5.4.4 Writing CLI tests · 5.5 Unit testing · 5.5.1 A helper type · 5.5.2 Unit-testing the parser · 5.6 Summary

What you learn in chapter 5 →

Chapter 6: Synchronous APIs for concurrency

6.1 Overview · 6.1.1 Package hit · 6.1.2 Package API · 6.1.3 Directory structure · 6.2 Foundations · 6.2.1 Result · 6.2.2 Send · 6.3 Iterators · 6.3.1 Push iterators · 6.3.2 Producing values · 6.3.3 Consuming values · 6.3.4 Testing · 6.4 Options · 6.4.1 Providing options · 6.4.2 Accepting options · 6.5 Integration · 6.5.1 Printing a summary · 6.5.2 Integration · 6.5.3 Demonstration · 6.6 Concurrent pipeline pattern · 6.6.1 Benefits of concurrent pipelines · 6.6.2 Designing a concurrent pipeline · 6.7 Producer stage · 6.7.1 Implementation · 6.7.2 Integration · 6.8 Throttler stage · 6.8.1 Implementation · 6.8.2 Integration · 6.9 Dispatcher stage · 6.9.1 Implementation · 6.9.2 Integration · 6.9.3 Demonstration · 6.9.4 Outro · 6.10 Exercises · 6.11 Summary

What you learn in chapter 6 →

Chapter 7: Responsive and efficient programs

7.1 Revisiting the concurrent pipeline · 7.2 Cancellation propagation · 7.2.1 What is Context? · 7.2.2 Context is like a tree · 7.2.3 Context in practice · 7.2.4 Deriving a new Context · 7.2.5 Is Ctrl+C the end? · 7.3 HTTP and efficient I/O operations · 7.3.1 Round-tripping · 7.3.2 Interface composition · 7.3.3 ReadAll: Eat it all · 7.3.4 Copy: Eat small, be small · 7.3.5 Putting it all together · 7.3.6 Demonstration · 7.4 Optimization · 7.4.1 Client and its RoundTripper · 7.4.2 Tweaking the connection pool · 7.4.3 Demonstration · 7.5 Testing · 7.5.1 Satisfying RoundTripper · 7.5.2 Testing with a RoundTripper · 7.6 HTTP testing · 7.6.1 Package httptest · 7.6.2 Testing the client · 7.7 Exercises · 7.8 Summary

What you learn in chapter 7 →

Chapter 8: Structuring packages and services

8.1 Organizing and structuring packages · 8.1.1 Avoiding import cycles · 8.1.2 Structuring packages in practice · 8.2 Core · 8.2.1 Errors · 8.2.2 Core · 8.2.3 Service · 8.2.4 Mutex · 8.3 HTTP · 8.3.1 Health check · 8.3.2 Serving HTTP · 8.3.3 HTTP server · 8.4 HTTP handlers · 8.4.1 Handler closures · 8.4.2 Redirecting · 8.4.3 HTTP status codes · 8.5 Routing · 8.5.1 ServeMux · 8.5.2 Routes · 8.5.3 Demonstration · 8.6 Timeouts · 8.7 Testing · 8.7.1 Response recording · 8.7.2 Testing a handler · 8.7.3 Test helpers · 8.8 Simplicity · 8.8.1 Up-front abstractions and indirections · 8.8.2 Inherently testable types · 8.9 Exercises · 8.10 Summary

What you learn in chapter 8 →

Chapter 9: Composition patterns

9.1 Middleware pattern · 9.1.1 What is HTTP middleware? · 9.1.2 Example · 9.1.3 Practice · 9.1.4 Learning more about the slog package and the any interface · 9.1.5 Integration · 9.2 Logging responses · 9.2.1 Measuring durations · 9.2.2 Response recording · 9.2.3 Integration · 9.3 Interceptor pattern · 9.3.1 Field embedding · 9.3.2 Capturing and saving · 9.3.3 Integration · 9.3.4 Demonstration · 9.4 Optional interface pattern · 9.4.1 Type asserting for optional functionality · 9.4.2 Unwrapping all the way down · 9.4.3 Unwrap · 9.5 Context value propagation pattern · 9.5.1 Generating, storing, and retrieving · 9.5.2 Middleware · 9.5.3 Wrapping slog handlers · 9.5.4 Implementing a slog.Handler · 9.5.5 Implementing WithAttrs and WithGroup · 9.5.6 Integration · 9.6 Handler-chaining pattern · 9.6.1 Chainable handlers · 9.6.2 Response handlers · 9.6.3 Responder · 9.6.4 Integration · 9.7 Encoding and decoding · 9.7.1 Encoding JSON · 9.7.2 Decoding JSON · 9.7.3 Speaking JSON · 9.8 Wrapping and unwrapping · 9.8.1 Safeguarding against denial-of-service attacks · 9.8.2 Unwrapping the original · 9.9 Outro · 9.10 Exercises · 9.11 Summary

What you learn in chapter 9 →

Chapter 10: Polymorphic storage

10.1 Interacting with SQL databases · 10.1.1 Registering a driver · 10.1.2 Opening a connection pool · 10.1.3 File embedding · 10.2 Database-backed service · 10.2.1 Insertion · 10.2.2 Test database · 10.2.3 Integration testing · 10.2.4 errors.As · 10.2.5 Retrieval · 10.3 Valuer and Scanner · 10.3.1 Supporting custom database types · 10.3.2 Satisfying Valuer and Scanner · 10.3.3 Encoding and decoding · 10.4 Implicit interfaces · 10.4.1 Consumers-first approach · 10.4.2 Providing an interface · 10.4.3 Activating the new implementation · 10.5 Exercises · 10.6 Summary

What you learn in chapter 10 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.