1

So, this works:

house := model.House {};
err = db.First(&house).Error;

However, this doesn't work:

var house model.House;

fetchFromDatabase := reflect.New(reflect.TypeOf(house)).Interface();
err = db.First(&fetchFromDatabase).Error;

... The database library gives the error:

unsupported destination, should be slice or struct

To me, that should be a struct, considering the "House" type is a struct. However, I'm still wrapping my head around Reflect ... can anyone help?

1 Answer 1

4

The library is complaining because the application is passing a *interface{} to the method. The value fetchFromDatabase is a pointer to a struct. Pass this value directly to the method:

var house model.House
fetchFromDatabase := reflect.New(reflect.TypeOf(house)).Interface()
err = db.First(fetchFromDatabase).Error
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.