Golang Use of array of structs that implement MyInterface as []MyInterface not allowed

22 views Asked by At

I can't use an array of structs that implement TheInterface as an argument for a method that receives []TheInterface.

I have the following scenario:

var earnings []Earning // where Earning implements the interface ValidableStep

for _, site := range sites {
  ... logic here
  earnings = append(earnings, siteEarning) // this works
}

# The below line produces this error: Cannot use 'earnings' (type []Earning) as the type []ValidableStep
validator.ValidateStep(earnings, "step 4")

where the definition of ValidateStep is

func ValidateStep(es []ValidableStep, customMsg string) {
...
}

Now I don't think I understand why this validator.ValidateStep(earnings, "step 4") is not allowed even though Earning is implementing ValidableStep so []Earning should be an implementation of []ValidableStep.

In any other programming language this would be trivial so I think I'm missing something in this case.

0

There are 0 answers