亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

填充作為指針傳遞給函數的結構數組

填充作為指針傳遞給函數的結構數組

Go
Helenr 2023-05-08 15:33:54
我想使用 Google Cloud Platform Datastore 進行數據分頁,我在 GCP 的頁面 ( https://cloud.google.com/datastore/docs/concepts/queries )上找到了一個使用 Cursors 進行分頁的示例,它可以正常工作絕對沒問題。Google 提供的示例對變量進行了硬編碼var tasks []Task, var task Task我想創建一個可重用的函數,我可以在其中通過鍵入的參數將指針傳遞給結構數組interface{},并通過該函數填充該結構。例如:  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)    }當我需要在此函數中創建一個變量來存儲記錄,然后將其附加到作為指針傳遞的結構數組時,我的問題就開始了。來自谷歌的例子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 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

使用反射包:


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.

}

使用指向目標切片的指針調用函數:


var data []Tasks

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


查看完整回答
反對 回復 2023-05-08
  • 1 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號