1 回答

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, "")
- 1 回答
- 0 關(guān)注
- 197 瀏覽
添加回答
舉報(bào)