1

I have a function which returns a slice of pointers of some interface. I want to change the type later in the code to the implementation type but nothing works, I still get an invalid type assertion.

Example

func Test(c Parsable)([]*Parsable, error) {
   // generate slice by factory method on Parsable inteface and return slice
}

var implParsable ImplParsable
results, err := Test(implParsable) 

data := results[0].(ImplParsable) // I tried this in many variations but nothing works
2
  • 3
    If it's a pointer you just need to dereference it (*), but are you sure you actually want to be using a pointer to to an interface? There is usually no ready to do so. Commented Aug 16, 2018 at 17:34
  • I try it also with * but not working, and I have function where I use it as factory, I add the function for more explanation of problem Commented Aug 16, 2018 at 17:37

1 Answer 1

3

resultSets[0] is a pointer to an interface, so you need to dereference that pointer to get the interface value, which you can do inline since slice values are addressable.

data :=  (*resultSets[0]).(ImplParsable)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.