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
You have to call Find with a pointer to the slice.
relevant Gorm documentation: http://jinzhu.me/gorm/crud.html#query
Just to add clarification to S.Diego answers, changing this:
to this:
the error says, the variable users is not addressable, because it is not a pointer.
In a very similar fashion to the accepted answer (but in a slightly different context), and an error that I keep making in different projects:
becomes
Notice the ampersands.