How do interfaces work in Go, and what is the empty interface?
Assesses fundamental understanding of Go conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
An interface is a set of method signatures. Any type that implements all of them satisfies the interface implicitly, with no implements keyword. Internally an interface value holds a pair of the concrete type and its value.
The empty interface, interface{} (now usually written any), declares no methods, so every type satisfies it. Before generics it was the standard way to hold heterogeneous data ([]any) and is still what encoding/json uses for arbitrary documents.
var v any = "hello"
s, ok := v.(string) // type assertion, comma-ok form
switch x := v.(type) { // type switch
case string:
fmt.Println(x)
case int:
fmt.Println(x)
}
A subtle and very common bug: an interface holding a typed nil pointer is not equal to nil, because the type component is set. Functions returning interfaces should return an explicit nil on the failure path. Small, focused interfaces are idiomatic; the saying is "accept interfaces, return structs".
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.