api: google.golang.org/api/iterator Index | Examples | Files | Directories

package iterator

import "google.golang.org/api/iterator"

Package iterator provides support for standard Google API iterators. See https://github.com/GoogleCloudPlatform/gcloud-golang/wiki/Iterator-Guidelines.

This example demonstrates how to use a Pager to page through an iterator in a loop.

Code:

// Find all primes up to 42, in pages of size 5.
const max = 42
const pageSize = 5
p := iterator.NewPager(Primes(max), pageSize, "" /* start from the beginning */)
for page := 0; ; page++ {
    var items []int
    pageToken, err := p.NextPage(&items)
    if err != nil {
        log.Fatalf("Iterator paging failed: %v", err)
    }
    fmt.Printf("Page %d: %v\n", page, items)
    if pageToken == "" {
        break
    }
}

Output:

Page 0: [2 3 5 7 11]
Page 1: [13 17 19 23 29]
Page 2: [31 37 41]

The example demonstrates how to use a Pager to request a page from a given token.

Code:

const pageSize = 5
const pageToken = "1337"
p := iterator.NewPager(Primes(0), pageSize, pageToken)

var items []int
nextPage, err := p.NextPage(&items)
if err != nil {
    log.Fatalf("Iterator paging failed: %v", err)
}
fmt.Printf("Primes: %v\nToken:  %q\n", items, nextPage)

Output:

Primes: [1361 1367 1373 1381 1399]
Token:  "1400"

This example demonstrates how to get exactly the items in the buffer, without triggering an extra RPC.

Code:

// The iterator returned by Primes has a default page size of 20, which means
// it will return all the primes in the range [2, 21).
it := Primes(0)
var items []int
for {
    item, err := it.Next()
    if err != nil && err != iterator.Done {
        log.Fatal(err)
    }
    if err == iterator.Done {
        break
    }
    items = append(items, item)
    if it.PageInfo().Remaining() == 0 {
        break
    }
}
fmt.Println(items)

Output:

[2 3 5 7 11 13 17 19]

This example demonstrates how to use Pager to support pagination on a web site.

Code:

const pageSize = 25
it := client.Items(ctx)
var items []int
pageToken, err := iterator.NewPager(it, pageSize, r.URL.Query().Get("pageToken")).NextPage(&items)
if err != nil {
    http.Error(w, fmt.Sprintf("getting next page: %v", err), http.StatusInternalServerError)
}
data := struct {
    Items []int
    Next  string
}{
    items,
    pageToken,
}
var buf bytes.Buffer
if err := pageTemplate.Execute(&buf, data); err != nil {
    http.Error(w, fmt.Sprintf("executing page template: %v", err), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if _, err := buf.WriteTo(w); err != nil {
    log.Printf("writing response: %v", err)
}

Index

Examples

Package Files

iterator.go

Variables

var Done = errors.New("no more items in iterator")

Done is returned by an iterator's Next method when the iteration is complete; when there are no more items to return.

var NewPageInfo = newPageInfo

NewPageInfo exposes internals for iterator implementations. It is not a stable interface.

type PageInfo Uses

type PageInfo struct {
    // Token is the token used to retrieve the next page of items from the
    // API. You may set Token immediately after creating an iterator to
    // begin iteration at a particular point. If Token is the empty string,
    // the iterator will begin with the first eligible item.
    //
    // The result of setting Token after the first call to Next is undefined.
    //
    // After the underlying API method is called to retrieve a page of items,
    // Token is set to the next-page token in the response.
    Token string

    // MaxSize is the maximum number of items returned by a call to the API.
    // Set MaxSize as a hint to optimize the buffering behavior of the iterator.
    // If zero, the page size is determined by the underlying service.
    //
    // Use Pager to retrieve a page of a specific, exact size.
    MaxSize int
    // contains filtered or unexported fields
}

PageInfo contains information about an iterator's paging state.

func (*PageInfo) Remaining Uses

func (pi *PageInfo) Remaining() int

Remaining returns the number of items available before the iterator makes another API call.

type Pageable Uses

type Pageable interface {
    // PageInfo returns paging information associated with the iterator.
    PageInfo() *PageInfo
}

Pageable is implemented by iterators that support paging.

type Pager Uses

type Pager struct {
    // contains filtered or unexported fields
}

Pager supports retrieving iterator items a page at a time.

func NewPager Uses

func NewPager(iter Pageable, pageSize int, pageToken string) *Pager

NewPager returns a pager that uses iter. Calls to its NextPage method will obtain exactly pageSize items, unless fewer remain. The pageToken argument indicates where to start the iteration. Pass the empty string to start at the beginning, or pass a token retrieved from a call to Pager.NextPage.

If you use an iterator with a Pager, you must not call Next on the iterator.

func (*Pager) NextPage Uses

func (p *Pager) NextPage(slicep interface{}) (nextPageToken string, err error)

NextPage retrieves a sequence of items from the iterator and appends them to slicep, which must be a pointer to a slice of the iterator's item type. Exactly p.pageSize items will be appended, unless fewer remain.

The first return value is the page token to use for the next page of items. If empty, there are no more pages. Aside from checking for the end of the iteration, the returned page token is only needed if the iteration is to be resumed a later time, in another context (possibly another process).

The second return value is non-nil if an error occurred. It will never be the special iterator sentinel value Done. To recognize the end of the iteration, compare nextPageToken to the empty string.

It is possible for NextPage to return a single zero-length page along with an empty page token when there are no more items in the iteration.

Directories

PathSynopsis
testingPackage testing provides support functions for testing iterators conforming to the standard pattern.

Package iterator imports 3 packages (graph) and is imported by 123 packages. Updated 2017-12-16. Refresh now. Tools for package owners.