I have a function that returns a product by its ID, but the problem is that it does not return it as a data array
func GetProductsById(c *gin.Context) {
var Product models.Products
if err := config.DB.Where("id=?", c.Query("prod_id")).First(&Product).Error; err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
} else {
c.JSON(http.StatusOK, gin.H{"data": &Product})
}
}
json response
{
"data": {
"ID": 1,
...
"Feedback": null
}
}
I want this:
{
"data": [{
"ID": 1,
...
"Feedback": null
}]
}
gin.H{"data": []models.Products{Product}}. If you're on Go1.18+ you can also dogin.H{"data": []any{Product}}.