tezvyn:

How does a Go type satisfy an interface? Provide an io.Reader example.

AI-drafted, machine-checkedSource: go.devbeginner

This tests implicit structural typing in Go. A type satisfies an interface by implementing every required method with exact signatures; no declaration links them. A red flag is claiming you need an implements keyword or explicit conformance.

WHAT THIS TESTS: The interviewer wants to know if you grasp Go's implicit interface satisfaction, also called structural typing. Unlike languages that require explicit implements clauses, Go uses duck typing enforced at compile time. The question checks whether you understand that interface conformance is a property of the methods attached to a type, not a declaration on the type itself. It also reveals whether you know that satisfaction is static and decouples definition from usage.

A GOOD ANSWER COVERS: First, state the rule clearly: a type satisfies an interface if it possesses every method in that interface with identical signatures, including exported or unexported names, parameter lists, and return types. Second, emphasize that this is implicit; the type does not need to announce which interfaces it implements, which promotes loose coupling. Third, mention that the compiler resolves satisfaction statically at build time, so there is no runtime penalty for the abstraction. Fourth, briefly describe the interface value representation, which is a pair of pointers holding the concrete value and its type metadata, since that shows deeper familiarity with how method dispatch works.

COMMON WRONG ANSWERS: Claiming that Go requires an implements keyword or that structs must list interfaces they satisfy. Confusing interface satisfaction with inheritance or subclassing. Saying that satisfaction is checked at runtime rather than compile time. Providing a code example where the method signature differs slightly, such as using a pointer receiver when the interface expects a value receiver, without explaining that T and pointer T are distinct types in Go. Asserting that embedding a struct automatically forwards interface satisfaction unless the embedded type is exported and promoted correctly.

LIKELY FOLLOW-UPS: The interviewer might ask what happens if a type implements some but not all methods of an interface, which produces a compile error only when you try to assign the type to the interface variable. They may ask about the difference between value and pointer receivers, so you should know that T and pointer T are different types and may satisfy different interfaces. They could also ask about the empty interface, any, and how it relates to type assertions or type switches. Another follow-up is whether a named type based on a built-in can satisfy an interface, which it can if the methods are defined on that named type.

ONE CONCRETE EXAMPLE: Imagine a struct named MyReader with a Read method that takes a byte slice and returns an int and an error. Because io.Reader requires exactly Read with a slice of byte parameter returning int and error, MyReader automatically satisfies io.Reader. You can assign a MyReader value to a variable of type io.Reader without any extra syntax. If you instead named the method ReadData or changed the parameter to a string, the assignment would fail to compile because the method set would no longer match. This demonstrates the strict signature matching that underlies Go's interface system.

Read the original → go.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.