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

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

填充作為指針傳遞給函數(shù)的結(jié)構(gòu)數(shù)組

填充作為指針傳遞給函數(shù)的結(jié)構(gòu)數(shù)組

Go
Helenr 2023-05-08 15:33:54
我想使用 Google Cloud Platform Datastore 進(jìn)行數(shù)據(jù)分頁,我在 GCP 的頁面 ( https://cloud.google.com/datastore/docs/concepts/queries )上找到了一個(gè)使用 Cursors 進(jìn)行分頁的示例,它可以正常工作絕對(duì)沒問題。Google 提供的示例對(duì)變量進(jìn)行了硬編碼var tasks []Task, var task Task我想創(chuàng)建一個(gè)可重用的函數(shù),我可以在其中通過鍵入的參數(shù)將指針傳遞給結(jié)構(gòu)數(shù)組interface{},并通過該函數(shù)填充該結(jié)構(gòu)。例如:  type MyStruct1 struct {        F1 string    }    type MyStruct2 struct {        F1 int    }    func list(ctx context.Context, cursorStr string, data interface{}) {    ...    }    func main() {        mystruct1 := make([]MyStruct1, 0)        list(ctx, "", &mystruct1)        mystruct2 := make([]MyStruct2, 0)        list(ctx, "", &mystruct2)    }當(dāng)我需要在此函數(shù)中創(chuàng)建一個(gè)變量來存儲(chǔ)記錄,然后將其附加到作為指針傳遞的結(jié)構(gòu)數(shù)組時(shí),我的問題就開始了。來自谷歌的例子func SnippetIterator_Cursor() {    ctx := context.Background()    client, _ := datastore.NewClient(ctx, "my-proj")    cursorStr := ""    // [START datastore_cursor_paging]    const pageSize = 5    query := datastore.NewQuery("Tasks").Limit(pageSize)    if cursorStr != "" {        cursor, err := datastore.DecodeCursor(cursorStr)        if err != nil {            log.Fatalf("Bad cursor %q: %v", cursorStr, err)        }        query = query.Start(cursor)    }    // Read the tasks.    var tasks []Task << THIS IS WHAT I WANT TO BE GENERIC     var task Task. << THIS IS WHAT I WANT TO BE GENERIC     it := client.Run(ctx, query)    _, err := it.Next(&task)    for err == nil {        tasks = append(tasks, task)        _, err = it.Next(&task)    }    if err != iterator.Done {        log.Fatalf("Failed fetching results: %v", err)    }    // Get the cursor for the next page of results.    nextCursor, err := it.Cursor()    // [END datastore_cursor_paging]    _ = err        // Check the error.    _ = nextCursor // Use nextCursor.String as the next page's token.}
查看完整描述

1 回答

?
慕運(yùn)維8079593

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

使用反射包:


func list(ctx context.Context, kind string, dst interface{}, pageSize int, cursorStr string) string {

? ? client, _ := datastore.NewClient(ctx, "my-proj")

? ? query := datastore.NewQuery(kind).Limit(pageSize)

? ? if cursorStr != "" {

? ? ? ? cursor, err := datastore.DecodeCursor(cursorStr)

? ? ? ? if err != nil {

? ? ? ? ? ? log.Fatalf("Bad cursor %q: %v", cursorStr, err)

? ? ? ? }

? ? ? ? query = query.Start(cursor)

? ? }


? ? // Get reflect value for the result slice.

? ? results := reflect.ValueOf(dst).Elem()


? ? // Allocate new value of the slice element type.?

? ? // resultp is pointer to that value.

? ? resultp := reflect.New(results.Type().Elem())


? ? it := client.Run(ctx, query)

? ? _, err := it.Next(resultp.Interface())

? ? for err == nil {

? ? ? ? // Append last value to results

? ? ? ? results.Set(reflect.Append(results, resultp.Elem())


? ? ? ? _, err = it.Next(resultp.Interface())

? ? }

? ? if err != iterator.Done {

? ? ? ? log.Fatalf("Failed fetching results: %v", err)

? ? }


? ? // Get the cursor for the next page of results.

? ? nextCursor, err := it.Cursor()

? ? // [END datastore_cursor_paging]

? ? _ = err? ? ? ? // Check the error.

? ? _ = nextCursor // Use nextCursor.String as the next page's token.

}

使用指向目標(biāo)切片的指針調(diào)用函數(shù):


var data []Tasks

cursor := list(ctx, "Tasks", &data, 10, "")


查看完整回答
反對(duì) 回復(fù) 2023-05-08
  • 1 回答
  • 0 關(guān)注
  • 197 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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