liveBook · Manning

Adapter

As explained in Go in Action, Second Edition

The adapter pattern presents an existing component through an interface compatible with the surrounding application's expectations.

An adapter presents an existing component through an interface the surrounding application expects. In a Go service, it forms the boundary between stable domain ports and volatile infrastructure libraries. section 11.1.2

The RSS reader adapter implements domain.Fetcher while keeping gofeed types inside the worker. It parses external feeds, converts Feed and Item values into domain.Feed and domain.Article values, and returns only domain objects to the application. section 11.3.3

A practical adapter design provides:

  • A compile-time assertion that catches interface mismatches before runtime. section 11.3.3
  • A replaceable implementation: tests can use a mock, while production can use RSSReader without changing consuming code. section 11.1.2

RSSReader at the worker boundary

Create RSSReader with gofeed.NewParser, call ParseURLWithContext, map feed metadata and items into domain types, then inject it into fetchAndStore as a domain.Fetcher. The worker can therefore fetch and store articles without depending directly on gofeed models.

How Go in Action, Second Edition covers it

How deep it goes
Demonstrates interface implementation, model translation, compile-time checking, dependency direction, and replacement, but not adapter variants.
Examples
Uses RSSReader, gofeed.Parser, domain.Fetcher, Feed and Article conversions, and worker fetch-and-store processing.
What it assumes
Assumes familiarity with Go interfaces, modules, context, and domain models while defining the architectural boundary concretely.
What you can do after
Build it from scratch by defining a domain port, implementing an infrastructure wrapper, translating models, and asserting interface compatibility.
How it connects
Connects ports and adapters with interfaces, dependency inversion, hexagonal architecture, domain models, testing, and dependency injection.

Also explained in

Compare how 9 books explain adapter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.