英文:
How can i put an object in object in golang
问题
我正在尝试的是将从数据库中加载的整个对象加载到一个对象中。
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
}
这段代码给我返回了以下结果:
[{
"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"
}]
现在我想做的是获取数组中的每个对象,并将其放入另一个对象中,期望的结果将是:
[{"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"
}}]
有没有办法实现这个目标?
英文:
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?
答案1
得分: 0
你可以循环并按以下方式执行:
type RatingPK struct {
ratingsPK Rating
}
func (r *RatingPK) Init(rg Rating) {
r.ratingsPK = rg
}
func searchUserRatings(id string) (ratings []RatingPK) {
rows, err := db.Query("SELECT * FROM ratings WHERE userId = ?", id)
if err != nil {
return
}
var myarray []string
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
}
英文:
You may loop and do as follows
type RatingPK struct {
ratingsPK Rating
}
func (r *RatingPK) Init(rg Rating) {
r.ratingsPK = rg
}
func searchUserRatings(id string) (ratings []RatingPK) {
rows, err := db.Query("SELECT * FROM ratings WHERE userId = ?", id)
if err != nil {
return
}
var myarray []string
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
}
答案2
得分: 0
以下是一个简短的示例,将帮助您了解如何使用嵌套结构体
package main
import (
"fmt"
)
type nameType struct {
firstName string
lastName string
}
type student struct {
name nameType
age int
}
func main() {
bob := student{nameType{"bob", "dope"}, 18}
fmt.Println(bob.name.firstName)
}
您可以在此处进行测试
https://play.golang.org/p/ft9PGrlrrx
英文:
Here's a short example that will help you understand how to use nested structs
package main
import (
"fmt"
)
type nameType struct {
firstName string
lastName string
}
type student struct {
name nameType
age int
}
func main() {
bob := student{nameType{"bob", "dope"}, 18}
fmt.Println(bob.name.firstName)
}
You can test it up here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论