I want to write a function that retrieves a record in the database with GORM where the destination struct is dynamic. Something like this:
type User struct {
ID uint
Email string
}
type BigUser struct {
ID uint
Username string
Role string
}
func GetRecord(db *gorm.DB, u SomeUserInterfaceTypeOrSomething) {
db.Find(&u)
}
GetRecord(db, User{})
GetRecord(db, BigUser{})
So if I pass a User struct variable I get only the ID and the email, if I pass a BigUser struct variable I get all the database fields (for example).
I need this because the GetRecord will be in a library and I want that who will be using the library can pass a custom struct to retrieve only the fields he wants.
I don't know how to achieve this, any advice, or best practice? I'm new with Golang :D