7 Responsive, efficient, and testable
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.
- Interface embedding and composition mechanics of Go.
- Idiomatic testing techniques using the RoundTripper interface.
- Testing against a test HTTP server using the httptest package.
Go is more than just learning basic language mechanics.
The path to fully grasping it also involves effectively using and understanding the patterns and techniques found in its standard library. Rather than learning from isolated code snippets that provide only a limited view of its capabilities, we continue to apply Go to projects that closely resemble real-world ones, allowing us to grasp Go and maximize its benefits fully.
In the last chapter, we 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 operated. 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.
