Go interfaces are implicit—no implements keyword like Java. A type satisfies an interface by having the required methods.
Defining and using interfaces
type Stringer interface {
String() string
}
The empty interface interface{} (alias any since Go 1.18) accepts any type—use sparingly; prefer generics or concrete types.
Interface values
An interface value holds a dynamic type and dynamic value. Type assertions v.(T) extract concrete types; the comma-ok form handles failures safely.
Important interview questions and answers
- Q: Implicit vs explicit interfaces?
A: Go uses implicit satisfaction—decouples packages and enables small, focused interfaces. - Q: nil interface gotcha?
A: An interface is nil only if both type and value are nil—a typed nil pointer inside an interface is not nil.
Self-check
- How does a type satisfy an interface?
- What does the comma-ok type assertion do?
Tip: Keep interfaces small—io.Reader has one method; define interfaces at the consumer, not the implementer.
Interview prep
- Explicit implements?
No—types satisfy interfaces implicitly by having required methods.
- Nil interface gotcha?
Interface is nil only when both type and value are nil—a typed nil pointer inside an interface is not nil.