'golang: BaseRepository usage with different struct return type [duplicate]

I have the following code with two repositories that have the same functionality but different return types. I want to call the findAll method from the BaseRepository to prevent writing the same logic for fetching entries multiple times. I want to keep different return types. Is this possible?

Currently I get the following error (in the UserRepository): // Error: cannot use r.baseRepository.FindAll(user) (value of type []interface{}) as []User

I understand the problem, but is there an easy solution for that? Maybe I have to use another approach?

package main

type Address struct {
    street string
}

type User struct {
    Firstname string
    Lastname  string
}

// BaseRepository
type BaseRepository struct{}

func (r *BaseRepository) FindAll(dest interface{}) []interface{} {
    // Return []Address or []User
    return []interface{}{}
}

// UserRepository
type UserRepository struct {
    baseRepository *BaseRepository
}

func (r *UserRepository) FindAll() []User {
    var user User
    return r.baseRepository.FindAll(user) // Error: cannot use r.baseRepository.FindAll(user) (value of type []interface{}) as []User value in return statement
}

// AddressRepoistory
type AddressRepository struct {
    baseRepository *BaseRepository
}

func (r *AddressRepository) FindAll() []Address {
    var address Address
    return r.baseRepository.FindAll(address)  // Error: cannot use r.baseRepository.FindAll(address) (value of type []interface{}) as []Address value in return statement
}

func main() {
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source