如何在Golang中将一个对象放入另一个对象中

huangapple go评论93阅读模式
英文:

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

以下是一个简短的示例,将帮助您了解如何使用嵌套结构体 如何在Golang中将一个对象放入另一个对象中

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)
}

您可以在此处进行测试 如何在Golang中将一个对象放入另一个对象中

https://play.golang.org/p/ft9PGrlrrx

英文:

Here's a short example that will help you understand how to use nested structs 如何在Golang中将一个对象放入另一个对象中

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 如何在Golang中将一个对象放入另一个对象中

https://play.golang.org/p/ft9PGrlrrx

huangapple
  • 本文由 发表于 2017年2月5日 01:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/42043219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定