iterator

package
v2.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2024 License: BSD-3-Clause Imports: 2 Imported by: 238

Documentation

Overview

Package iterator contains helper for working with iterators. It is meant for internal use only by the Go Client Libraries.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RangeAdapter

func RangeAdapter[T any](next func() (T, error)) iter.Seq2[T, error]

RangeAdapter transforms client iterator type into a iter.Seq2 that can be used with Go's range expressions.

This is for internal use only.

Example
//go:build go1.23

package main

import (
	"fmt"
	"iter"

	"github.com/googleapis/gax-go/v2/iterator"
	otherit "google.golang.org/api/iterator"
)

type exampleType struct {
	data []int
	i    int
}

func (t *exampleType) next() (int, error) {
	var v int
	if t.i == len(t.data) {
		return v, otherit.Done
	}
	v = t.data[t.i]
	t.i++
	return v, nil
}

func (t *exampleType) All() iter.Seq2[int, error] {
	return iterator.RangeAdapter[int](t.next)
}

func main() {
	t := &exampleType{
		data: []int{1, 2, 3},
	}
	for v, err := range t.All() {
		if err != nil {
			// TODO: handle error
		}
		fmt.Println(v)
	}
}
Output:

1
2
3

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL