第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用“reflect”將數(shù)據(jù)附加到指向已定義結(jié)構(gòu)的接口

使用“reflect”將數(shù)據(jù)附加到指向已定義結(jié)構(gòu)的接口

Go
慕哥6287543 2023-08-07 15:28:39
我正在嘗試創(chuàng)建一個函數(shù),它從 Mongo 集合中獲取所有文檔并查詢它們以聲明的結(jié)構(gòu)。為了實現(xiàn)這一點,我設(shè)置了類型接口函數(shù)的參數(shù),以便它可以使用兩個結(jié)構(gòu)。這是我的代碼:在包實體中:type Project struct {    Title string    Position string    ....}type Projects struct {    Projects []Project}在當(dāng)前包中:var docs entities.Projectsvar doc entities.Project//doc represents a document from Mongo Collection //docs represents an array of documents, each element is a document//collection has type *mongo.Collection and points to the desired collection on MongoDB.createQuery(&doc, &docs, collection)func createQuery(doc interface{}, docs interface{}, c *mongo.Collection) {    documents := reflect.ValueOf(docs).Elem()    document := reflect.ValueOf(doc)    cur, err := c.Find(context.Background(), bson.D{{}})    if err != nil {        log.Fatal(err)    }    for cur.Next(context.Background()) {        err = cur.Decode(document.Interface())        if err != nil {            log.Fatal(err)        }        //Error is thrown here        documents.Set(reflect.Append(documents, document))        fmt.Println(doc)    }    if err := cur.Err(); err != nil {        log.Fatal(err)    }    if err != nil {        fmt.Printf("oh shit this is the error %s \n", err)    }    cur.Close(context.Background())    fmt.Printf("documents: %+v\n", documents.Interface())    fmt.Printf("document: %+v\n", document.CanSet())}---ERROR OUTPUT---panic: reflect: call of reflect.Append on struct Value我能夠使用文檔變量將數(shù)據(jù)設(shè)置為 doc,盡管在執(zhí)行 document.CanSet() 時為 false(因此它甚至可能不起作用)。當(dāng)我嘗試將文檔附加到文檔界面時,程序就會中斷。
查看完整描述

1 回答

?
白衣染霜花

TA貢獻1796條經(jīng)驗 獲得超10個贊

問題中的代碼將結(jié)構(gòu)傳遞docs給需要切片的函數(shù)。傳遞切片字段的地址而docs不是docs其本身。


該createQuery函數(shù)可以從切片本身確定切片元素類型。無需傳遞示例值。


var docs entities.Projects

createQuery(&docs.Projects, collection)

for _, doc := range docs.Projects {

   fmt.Println(doc.Title)

}

調(diào)用cur.Decode需要一個指向未初始化值的指針。使用reflect.New 來創(chuàng)建該值。


func createQuery(docs interface{}, c *mongo.Collection) {

    docsv := reflect.ValueOf(docs).Elem()

    doct := docsv.Type().Elem()


    cur, err := c.Find(context.Background(), bson.D{{}})

    if err != nil {

        log.Fatal(err)

    }


    for cur.Next(context.Background()) {

        docpv := reflect.New(doct)

        err = cur.Decode(docpv.Interface())

        if err != nil {

            log.Fatal(err)

        }

        docsv.Set(reflect.Append(docsv, docpv.Elem()))

    }

    if err := cur.Err(); err != nil {

        log.Fatal(err)

    }

    cur.Close(context.Background())

}

順便說一句,entities.Projects如果該結(jié)構(gòu)類型只有一個字段,則不需要該結(jié)構(gòu)類型。改用[]Project:


var docs []entities.Project

createQuery(&docs, collection)

for _, doc := range docs {

   fmt.Println(doc.Title)

}


查看完整回答
反對 回復(fù) 2023-08-07
  • 1 回答
  • 0 關(guān)注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號