It's not clear how the logic goes here, but one common pattern in Go is encapsulating the shared functionality in another struct type and then embed it in your type:
type sharedFunctionality struct{}
func (*sharedFunctionality) Do() {}
type type1 struct{ sharedFunctionality }
type type2 struct{ sharedFunctionality }
Now you can call Do()
on type1
and type2
instances or in any other type that you need this functionality.
Edit: based on your comment, you can just redefine some equivalent types such as t1
and t2
that follow the desired protocol (having a Do()
method) like this:
func main() {
var j job
j = new(t1)
j.Do()
j = new(t2)
j.Do()
}
type job interface {
Do()
}
type t1 another.Type1
func (*t1) Do() {}
type t2 yetanother.Type2
func (*t2) Do() {}
Here the types another.Type1
and yetanother.Type2
are not defined by you, but rather some other package designer. But you can do whatever the logic demands with t1
and t2
- as far as public members go or if you are willing to mess with that reflection thing :)