[英]Get inside struct values
In this example , I changed the Sql driver
to mgo
, I got some confusion. 在这个例子中 ,我将Sql driver
更改为mgo
,我有些困惑。 Here, How can I access the inside struct value? 在这里,我如何访问内部struct值? like nested struct. 喜欢嵌套的结构。
I have two struct, 我有两个结构,
Author 作者
type Author struct {
ID string `bson:"id"`
Name string `bson:"name"`
Timestamp time.Time
}
Article 文章
type Article struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Title string `bson:"title"`
Content string `bson:"content"`
Author Author `bson:"inline"`
Timestamp time.Time
}
Function 功能
func (m *mgoArticleRepository) FindAll() ([]*models.Article, error) {
result := make([]*models.Article, 0)
sessionCopy := m.Conn.Copy()
defer sessionCopy.Close()
collection := sessionCopy.DB(DBNAME).C(COLLECTION)
err := collection.Find(nil).All(&result)
return result, err
}
Output 产量
Author object return empty (I have data in mongoDB) 作者对象返回空(我在mongoDB中有数据)
[
{
"ID":"5b4f27c187a9e40828422cca",
"Title":"Makan Ayam",
"Content":"Sample values One",
"Author":{
"ID":"",
"Name":"",
"Timestamp":"0001-01-01T00:00:00Z"
},
"Timestamp":"0001-01-01T00:00:00Z"
},
{
"ID":"5b4f27c187a9e40828422ccb",
"Title":"Makan Ikan",
"Content":"Sample values Two",
"Author":{
"ID":"",
"Name":"",
"Timestamp":"0001-01-01T00:00:00Z"
},
"Timestamp":"0001-01-01T00:00:00Z"
},
{
"ID":"5b4f27c187a9e40828422ccc",
"Title":"Makan Sayur",
"Content":"Sample values Three",
"Author":{
"ID":"",
"Name":"",
"Timestamp":"0001-01-01T00:00:00Z"
},
"Timestamp":"0001-01-01T00:00:00Z"
},
{
"ID":"5b4f27c187a9e40828422ccd",
"Title":"Makan Daging",
"Content":"Sample values Four",
"Author":{
"ID":"",
"Name":"",
"Timestamp":"0001-01-01T00:00:00Z"
},
"Timestamp":"0001-01-01T00:00:00Z"
},
{
"ID":"5b4f27c187a9e40828422cce",
"Title":"Makan Indomie",
"Content":"Sample values Five",
"Author":{
"ID":"",
"Name":"",
"Timestamp":"0001-01-01T00:00:00Z"
},
"Timestamp":"0001-01-01T00:00:00Z"
},
{
"ID":"5b4f27c187a9e40828422ccf",
"Title":"Makan Soto",
"Content":"Sample values Six",
"Author":{
"ID":"",
"Name":"",
"Timestamp":"0001-01-01T00:00:00Z"
},
"Timestamp":"0001-01-01T00:00:00Z"
}
]
Here, How can I make the relationship for both structs? 在这里,我如何建立两个结构的关系? Thanks. 谢谢。
change line: 改变线:
Author Author `bson:"inline"
to 至
Author Author `bson:"Author"`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.