Как вариант, мне подсказали решение через дженерики
package main
import "fmt"
//import repository
type RepositoryInterface[T SubrepoInterface] interface {
GetSubrepo() T
}
type SubrepoInterface interface {
HelloSubrepo()
}
func main() {
r := &Repository{subrepo: &Subrepo{}}
useRepository[*Subrepo](r)
}
func useRepository[T SubrepoInterface](r RepositoryInterface[T]) {
sr := r.GetSubrepo()
sr.HelloSubrepo()
}
// ******************** Repository package ****************
type Repository struct {
subrepo *Subrepo
}
func (r *Repository) GetSubrepo() *Subrepo {
return r.subrepo
}
type Subrepo struct {
}
func (sr *Subrepo) HelloSubrepo() {
fmt.Printf("hello subrepo\n")
}