What happens when you call a value-receiver method through an interface that holds a nil pointer?
type V struct{ n int }
func (v V) Get() int { return 42 } // value receiver, never reads v.n
type Getter interface{ Get() int }
//go:noinline
func call(g Getter) int { return g.Get() }
func main() {
var p *V
var g Getter = p // legal: *V's method set includes V's methods
fmt.Println(g != nil) // ?
fmt.Println(call(g)) // ?
}
Output:
true
panic: value method main.V.Get called using nil *V pointer
*V satisfies Getter because the method set of *V includes value-receiver methods. The compiler makes this work by generating a wrapper method (*V).Get that dereferences the pointer, copies the V and calls V.Get. The itab points at that wrapper. When the pointer is nil, the wrapper cannot make the copy, so it calls runtime.panicwrap. That happens even though Get never touches a field.
A pointer-receiver method, by contrast, is called with a nil receiver and can handle it (as (*bytes.Buffer).String does). If the compiler can see the concrete type and devirtualizes the call, or if you call p.Get() directly, you get the ordinary invalid memory address or nil pointer dereference panic instead.
The interviewer is checking that you know value methods are reachable from pointers only through auto-generated wrappers, and that a method designed to be nil-safe needs a pointer receiver.
More on Interfaces, Methods & Embedding
- Q105Do methods carry over with
type B Avstype B = Avs embedding? What does this mean forsync.Mutex? - Q106How does
http.HandlerFuncwork? Explain the function-type adapter pattern. - Q108Can you modify a struct stored inside an interface? What does this print?
- Q109How do you write a generic function that needs to call a pointer-receiver method on
T? (thePT interface{ *T; M() }pattern) - Q110How do you "seal" an interface so other packages cannot implement it? Can the seal be bypassed?
- Q111How do you check at runtime whether a type implements an interface using reflection? Why is
reflect.TypeOfawkward for interface types?