Closed felix-h closed 4 years ago
Changing "func (embeddedStruct *EmbeddedStruct) Validate() error" for "func (embeddedStruct EmbeddedStruct) Validate() error" fixes the problem.
This is because EmbeddedStruct
within Outer
is not a pointer. As a result, it's not implementing the Validatable
interface because you associate Validate()
with a pointer of EmbeddedStruct
. This is golang rule. You can use the approach you described to fix the problem. You may also use *EmbeddedStruct
within Outer
.
When using embedded structs, the Validate() method of an embedded struct is not getting called when using a pointer receiver for the Validate() method.
https://play.golang.org/p/cLWuqkP8-mY
This prints: "Validating outer struct Program exited."
Expected output: "Validating outer struct I am not getting printed :( Program exited."
FWIW, the problem might be the type assertion in validation.go which fails for embedded structs:
if v, ok := value.(Validatable); ok { return v.Validate() }