When i call a function with an interface{}
parameters with a *[]interface{}
, the behavior is expected, but when I call the function with []interface{}
, and then use the parameter with &
it doesn't work why ?
func routeWarehouses(engine *gin.Engine) {
var test []database.Warehouses
router.GET("/", genericReads(test))
}
func genericReads(i interface{}) func(c *gin.Context) {
return func(c *gin.Context) {
// When i call genericReads with `test`
//println(reflect.TypeOf(i).Kind()) // Slice
//println(reflect.TypeOf(i).Elem().Kind()) // Struct
// When i call genericReads `&test`
//println(reflect.TypeOf(i).Kind()) // Ptr
//println(reflect.TypeOf(i).Elem().Kind()) // Slice
//println(reflect.TypeOf(i).Elem().Elem().Kind()) // Struct
// When I call database.Reads with `i` ( passed as `&test` ), It's works, I get all rows of the Model otherwise
// When I call database.Reads with `&i` ( passed as `test` ), It doesn't work ( I get `unsupported destination, should be slice or struct` )
if err := database.Reads(&i, database.Warehouses{}); err != nil {
utils.R500(c, err.Error())
return
}
c.JSON(http.StatusOK, i)
}
}
func Reads(i interface{}, column ColumnSpell) error {
if err := DB.Debug().Find(i).Error; err != nil {
return errors.New(fmt.Sprintf("Cannot reads %s: %s", column.Plural(), err.Error()))
}
return nil
}
PS: Maybe this come directly from Gorm ?