[英]Golang changing values of a struct inside a method of another struct
[英]Get inside struct values
在這個例子中 ,我將Sql driver
更改為mgo
,我有些困惑。 在這里,我如何訪問內部struct值? 喜歡嵌套的結構。
我有兩個結構,
作者
type Author struct {
ID string `bson:"id"`
Name string `bson:"name"`
Timestamp time.Time
}
文章
type Article struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Title string `bson:"title"`
Content string `bson:"content"`
Author Author `bson:"inline"`
Timestamp time.Time
}
功能
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
}
產量
作者對象返回空(我在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"
}
]
在這里,我如何建立兩個結構的關系? 謝謝。
改變線:
Author Author `bson:"inline"
至
Author Author `bson:"Author"`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.