reflect.Value.Set using unaddressable value

Krishna Satya picture Krishna Satya · Aug 22, 2017 · Viewed 18.6k times · Source
g.GET("/", func(c echo.Context) error {
    var users []models.User
    err := db.Find(users).Error
    if err != nil {
        fmt.Println(err)
    }
    return c.JSON(http.StatusOK, users)
})

this is the code for getting and displaying users from table using slice is resulting following error from gorm

reflect.Value.Set using unaddressable value

Answer

S. Diego picture S. Diego · Aug 22, 2017

You have to call Find with a pointer to the slice.

err := db.Find(&users).Error

relevant Gorm documentation: http://jinzhu.me/gorm/crud.html#query