What i am trying to do is to load an whole object coming from my database into an object.
type RatingPK struct {
ratingsPK Rating
}
func (r *RatingPK) Init(rg Rating) {
r.ratingsPK = rg
}
func searchUserRatings(id string)(ratings []Rating) {
rows, err := db.Query("SELECT * FROM ratings WHERE userId = ?", id)
if err != nil {
return
}
for rows.Next() {
rate := Rating{}
err = rows.Scan(&rate.MovieId, &rate.UserId, &rate.RatingId, &rate.Rating, &rate.Timestamp)
if err != nil {
return
}
r := RatingPK{}
r.Init(rate)
ratings = append(ratings, r)
}
defer rows.Close()
return
}
this code gives me this result:
[{
"rating_id": 593,
"userId": 2,
"movieId": 93,
"rating": 3,
"timestamp": "0000-00-00"
},
{
"rating_id": 616,
"userId": 2,
"movieId": 94,
"rating": 3,
"timestamp": "0000-00-00"
}]
Now what i want to do is to get each object of the array and put in to another object and the desired result will be this:
[{"ratingsPK": {
"rating_id": 593,
"userId": 2,
"movieId": 93,
"rating": 3,
"timestamp": "0000-00-00"
}},
{"ratingsPK": {
"rating_id": 616,
"userId": 2,
"movieId": 94,
"rating": 3,
"timestamp": "0000-00-00"
}}]
Is there any way to achieve that?