This chapter covers
- Effectively using composition patterns and functional programming techniques
- Using field embedding and method forwarding to reuse functionality
- Understanding optional interfaces, method-hiding issues, and type assertions
- Propagating request-scoped values across package APIs using
Context
- Implementing custom
slog.Handler
s to log extra attributes automatically - Wrapping interfaces to modify, extend, and intercept existing behavior
- Extracting implicit behavior using anonymous interfaces
In chapter 8, we implemented a link service, discussed structuring packages, and developed a REST API over HTTP. In this chapter, we’ll extend our project, focusing on composition.
In his “Less is exponentially more” article (https://mng.bz/JwN0), Rob Pike says, “If C++ and Java are about type hierarchies and the taxonomy of types, Go is about composition.” We may be used to inheritance-based designs, large frameworks, or large manager types when we come from other languages. By contrast, Go’s philosophy is similar to UNIX’s: do one thing well. Our packages provide simple, reusable pieces that allow users to compose them together on top of what we provide.