The simple answer is that you won't be able to have the struct implement your interface while having SetSomeField
work the way you want.
However, a pointer to the struct will implement the interface, so changing your Create
method to do return &obj
should get things working.
The underlying problem is that your modified SetSomeField
method is no longer in the method set of Implementation
. While the type *Implementation
will inherit the non-pointer receiver methods, the reverse is not true.
The reason for this is related to the way interface variables are specified: the only way to access the dynamic value stored in an interface variable is to copy it. As an example, imagine the following:
var impl Implementation
var iface IFace = &impl
In this case, a call to iface.SetSomeField
works because it can copy the pointer to use as the receiver in the method call. If we directly stored a struct in the interface variable, we'd need to create a pointer to that struct to complete the method call. Once such a pointer is made, it is possible to access (and potentially modify) the interface variable's dynamic value without copying it.