Chapter 9: Composition patterns

June 24, 2025 ยท View on GitHub

This chapter focuses on composition patterns to demonstrate how to design packages that offer reusable and composable functionality.

Topics covered

  • Effectively using composition patterns and functional programming techniques
  • Leveraging 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.Handlers to log extra attributes automatically
  • Wrapping interfaces to modify, extend, and intercept existing behavior
  • Extracting implicit behavior using anonymous interfaces

Sections

Section 1: Middleware pattern

Section 2: Logging responses

Section 3: Interceptor pattern

Section 4: Optional interface pattern

Section 5: Context value propagation pattern

Section 6: Handler chaining pattern

Section 7: Encoding and decoding

Section 8: Wrapping and unwrapping

Section 9: Outro

  • This section has no listings.

Section 10: Extra (not included in the book)