This chapter covers
- Propagating cancellation signals using the
context
package - Efficiently processing HTTP using the
net/http
package - Efficiently consuming byte streams using the
io
package - Understanding Go’s interface embedding and composition mechanics
- Employing idiomatic testing techniques using the
RoundTripper
interface - Testing against a test HTTP server using the
httptest
package
Using Go involves more than learning basic language mechanics. The path to fully grasping it also involves effective use and understanding of the patterns and techniques in its standard library. Rather than learn from isolated code snippets that provide a limited view of Go’s capabilities, we’ll continue to apply Go to projects that closely resemble real-world ones.
7.1 Revisiting the concurrent pipeline
Chapter 6 focused on designing a synchronous API for the hit
package and structuring concurrent code using the concurrent pipeline pattern. Figure 7.1 shows how the pipeline operates. The SendN
function returns a Results
iterator that sends concurrent requests using the Send
function and pushes each Result
to consumers. The convenience function, Summarize
, consumes each Result
from the iterator and produces a Summary
.
Figure 7.1 SendN
runs a concurrent pipeline that simulates sending requests using Send
. It returns a Results
iterator that pushes each Result
from the pipeline to consumers. Summarize
consumes the iterator and produces a Summary
.
