liveBook · Manning

Compile-time interface check

As explained in Go in Action, Second Edition

A compile-time interface check is a declaration or construct that makes the compiler verify that a type satisfies a specified interface.

A compile-time interface check makes Go verify that a concrete type satisfies an interface before the program runs. The idiomatic declaration `var _ domain.Fetcher = (*RSSReader)(nil)` turns an adapter contract into a compiler-enforced guarantee. section 11.3.3

Checking an RSS adapter

Place `var _ domain.Fetcher = (*RSSReader)(nil)` beside `RSSReader`. If the type later omits or changes a required `FetchFeed` method, compilation fails immediately. The adapter can then transform gofeed values into domain types without exposing the external library throughout the application. section 11.3.3

How Go in Action, Second Edition covers it

How deep it goes
Shows the assertion in a working adapter and stops short of explaining compiler internals.
Examples
Uses an RSSReader implementing domain.Fetcher while converting gofeed values into domain models.
What it assumes
Assumes familiarity with Go interfaces, concrete types, pointers, and method sets.
What you can do after
Diagnose interface mismatches immediately through compiler errors instead of discovering them at runtime.
How it connects
Connects interfaces with adapters, domain types, external libraries, and dependency wiring.

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.