What I am trying to do: I have several struct types, all implementing the same interface, which declare a method, say "Process()"
type Worker interface {
Process()
}
type obj1 struct {
}
func (o *obj1) Process() {
}
// same with struct type obj2, obj3, ...
Throughout the code, I create several instances of these structs, and then I want to call the process on each of these. Calling Process() on each works fine.
o1 := obj1.New()
o2 := obj1.New()
o3 := obj2.New()
o4 := obj3.New()
// ...
o1.Process()
o2.Process()
// ...
Now, I would like to have a "ProcessAll()" function, that would receive those instances and do the job of calling the Process() method on each of those, as well as some meta work around it.
Here is the sort of code that I'm trying to produce, but this particular snippet does not work because I'm not sure how to do it:
func ProcessAll(objs []*Worker) {
for _, obj := range objs {
obj.Process()
}
}
ProcessAll([]*Worker{o1, o2, /* ... */})
Is that sort of things possible to accomplish with go, and if yes how can I do to implement it ?