Value versus pointer receivers and interface satisfaction
WHAT IT TESTS: method sets and interface satisfaction. OUTLINE: value-receiver methods belong to both T and *T, but pointer-receiver methods belong only to *T, so a value of T may not satisfy an interface.
WHAT IT TESTS: whether you understand how receiver type shapes the method set and thus interface satisfaction. ANSWER OUTLINE: a value receiver operates on a copy and its method is in the method set of both T and *T; a pointer receiver can mutate the original and its method is in the method set of *T only. Therefore a value of type T satisfies an interface only if all required methods have value receivers; if any uses a pointer receiver you must pass *T.
Read the original → interview
- #go
- #interfaces
- #method-sets
- #pointer-receiver
- #value-receiver
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.