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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如果數(shù)組作為 &val 傳遞然后轉(zhuǎn)換為 interface{},則更新數(shù)組元素

如果數(shù)組作為 &val 傳遞然后轉(zhuǎn)換為 interface{},則更新數(shù)組元素

Go
MM們 2023-07-04 14:49:29
我正在嘗試編寫(xiě)一些通用方法(CRUD 方法)來(lái)在我的服務(wù)之間共享它。以下示例是一個(gè)GetAll()返回我的集合中存在的所有文檔的方法:func GetAll(out interface{}) error {    // mongodb operations    // iterate through all documents    for cursor.Next(ctx) {        var item interface{}        // decode the document        if err := cursor.Decode(&item); err != nil {            return err        }        (*out) = append((*out), item)        // arrays.AppendToArray(out, item) // Read below :)    }    return nil // if no error}我也嘗試過(guò)一些反思,但后來(lái):package arraysimport "reflect"func AppendToArray(slicePtrInterface interface{}, item interface{}) {    // enter `reflect`-land    slicePtrValue := reflect.ValueOf(slicePtrInterface)    // get the type    slicePtrType := slicePtrValue.Type()    // navigate from `*[]T` to `T`    _ = slicePtrType.Elem().Elem() // crashes if input type not `*[]T`    // we'll need this to Append() to    sliceValue := reflect.Indirect(slicePtrValue)    // append requested number of zeroes    sliceValue.Set(reflect.Append(sliceValue, reflect.ValueOf(item)))}恐慌:reflect.Set:類(lèi)型primitive.D的值不可分配給類(lèi)型*mongodb.Test [已恢復(fù)] 恐慌:reflect.Set:類(lèi)型primitive.D的值不可分配給類(lèi)型*mongodb.Test我想要的是采用與cursor.Decode(&item)(您可以在上面看到的)相同的方法
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

操作方法如下:


// GetAll decodes the cursor c to slicep where slicep is a 

// pointer to a slice of pointers to values.

func GetAll(ctx context.Context, c *Cursor, slicep interface{}) error {

    // Get the slice. Call Elem() because arg is pointer to the slice.

    slicev := reflect.ValueOf(slicep).Elem()


    // Get value type. First call to Elem() gets slice 

    // element type. Second call to Elem() dereferences 

    // the pointer type.

    valuet := slicev.Type().Elem().Elem()


    // Iterate through the cursor...

    for c.Next(ctx) {

        // Create new value.

        valuep := reflect.New(valuet)


        // Decode to that value.

        if err := c.Decode(valuep.Interface()); err != nil {

            return err

        }


        // Append value pointer to slice.

        slicev.Set(reflect.Append(slicev, valuep))

    }

    return c.Err()

}

像這樣稱(chēng)呼它:


var data []*T

err := GetAll(ctx, c, &data)

if err != nil {

   // handle error

}

在 Go Playground 上運(yùn)行它

以下是處理非指針切片元素的代碼的概括:

func GetAll(ctx context.Context, c *Cursor, slicep interface{}) error {

    slicev := reflect.ValueOf(slicep).Elem()

    valuet := slicev.Type().Elem()

    isPtr := valuet.Kind() == reflect.Ptr

    if isPtr {

        valuet = valuet.Elem()

    }

    for c.Next(ctx) {

        valuep := reflect.New(valuet)

        if err := c.Decode(valuep.Interface()); err != nil {

            return err

        }

        if !isPtr {

            valuep = valuep.Elem()

        }

        slicev.Set(reflect.Append(slicev, valuep))

    }

    return c.Err()

}


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號(hào)

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