gpt4 book ai didi

go - 获取内部结构值

转载 作者:IT王子 更新时间:2023-10-29 01:14:29 25 4
gpt4 key购买 nike

在此example ,我将 Sql driver 更改为 mgo,我有些困惑。在这里,如何访问内部结构值?像嵌套结构。

我有两个结构,

作者

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
}

输出

Author 对象返回空(我在 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"`

关于go - 获取内部结构值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com