Because the Print
method is on the pointer receiver, the type Test
does not implement PrintStr
.
Here are two options for fixing the problem.
Option 1: Use the address of the field. The value of values.Field(i).Addr().Interface()
is a *Test
, which does implement the interface.
switch v := values.Field(i).Addr().Interface().(type)
https://go.dev/play/p/ElJamDeiwSB
Option 2: Declare method on value receiver. With this change, type Test
implements the interface.
func (t Test) Print() {