Pagination

Listing methods return lazy iterators. Use for resource, err := range listing.All() to read the results. Calling the listing method or All() makes no API request. The iterator fetches pages during iteration. All() does not collect all pages first.

Each resource has a nil error. Check the error before using the resource. An unhandled request, decoding, or cancellation error is yielded once and ends that traversal. An empty listing, normal completion, or a page limit ends the loop without an extra resource or error.

Set ListParams.PerPage to request a page size. Set ListParams.MaxPages to limit the number of pages requested. Its default value, 0, allows all pages.

Range Over Results

import (
    "fmt"
    "errors"

    files_sdk "github.com/Files-com/files-sdk-go/v3"
    "github.com/Files-com/files-sdk-go/v3/folder"
)

listing, err := folder.ListFor(files_sdk.FolderListForParams{Path: "path"})
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Printf("API error (%s): %s\n", respErr.Type, respErr.ErrorMessage)
    } else {
        fmt.Printf("Request failed: %v\n", err)
    }
    return
}

for file, err := range listing.All() {
    if err != nil {
        var respErr files_sdk.ResponseError
        if errors.As(err, &respErr) {
            fmt.Printf("API error (%s): %s\n", respErr.Type, respErr.ErrorMessage)
        } else {
            fmt.Printf("Request failed: %v\n", err)
        }
        return
    }
    fmt.Println(file.Path)
}

Stop or Resume Iteration

Use break or return to stop the loop. No further results are yielded or pages requested by that traversal.

The listing is single-use. All() continues from its current position instead of restarting it. After earlier Next() calls or an early break, another All() loop or Next() call resumes at the next resource. After completion, reaching MaxPages, or a failure, later All() calls yield nothing and make no requests. Err() retains a failure, including one encountered through Next().

Call the listing method again to restart. Reload() also creates a fresh iterator, returned as files_sdk.IterI. Use a type assertion to the generated iterator type if you want to call All() on that result.

Both All() and Next() advance the same iterator. Use one traversal at a time. Do not call Next() inside an All() loop or use one iterator from several goroutines.

Use Next and Err

Existing Next() and Err() code remains supported. Read the typed resource, such as File(), only after Next() returns true. Check Err() after the loop.

Next and Err

import (
    "fmt"
    "errors"

    files_sdk "github.com/Files-com/files-sdk-go/v3"
    "github.com/Files-com/files-sdk-go/v3/folder"
)

listing, err := folder.ListFor(files_sdk.FolderListForParams{Path: "path"})
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Printf("API error (%s): %s\n", respErr.Type, respErr.ErrorMessage)
    } else {
        fmt.Printf("Request failed: %v\n", err)
    }
    return
}

for listing.Next() {
    fmt.Println(listing.File().Path)
}
err = listing.Err()
if err != nil {
    var respErr files_sdk.ResponseError
    if errors.As(err, &respErr) {
        fmt.Printf("API error (%s): %s\n", respErr.Type, respErr.ErrorMessage)
    } else {
        fmt.Printf("Request failed: %v\n", err)
    }
    return
}