Argument expansion and interface types

I might be running into the limits of what the language allows here, but I am trying to solve the following problem:

I have a slice of structs of type TestObject, which implements the interfaces TestInterface1 and TestInterface2
These interfaces are conceptually allow merging, and I have implemented two functions that implement this merging:

func mergeInterface1(TestInterface1...) TestInterface1
func mergeInterface2(TestInterface2...) TestInterface2

For the code I am writing I need to create both merges, so I tried

var list []TestObject
m1 := mergeInterface1(list…)
m2 := mergeInterface2(list…)

This obviously doesn’t work, as the types don’t match (the expansion is to the actual struct values, interface type values are always pointers so far as I can tell). However, when trying to then first create a list of pointers instead, like

var listPtrs []*TestObject
m1 := mergeInterface1(list…)
m2 := mergeInterface2(list…)

this still won’t compile, complaining about a type issue, even though I have verified that TestObject does implement the interfaces. Is there a nice way of doing the parameter expansion in a situation like this, or should I just bit the bullet and convert into two separate lists, one for each interface type?

You need to create the slices.

https://golang.org/doc/faq#convert_slice_of_interface

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.