需要在Golang中使用Reflect构造接口/指针,无法正常工作

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?

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