79614316

Date: 2025-05-09 14:24:19
Score: 1
Natty:
Report link

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() {

https://go.dev/play/p/uAfWzYzBmiX

Reasons:
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Jasmine Crockett