Documentation ¶
Overview ¶
Package iter provides a generic Iterator pattern, which is reduced to a minimum. This package is questionable and should be replaced until a stdlib contract approaches.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
Done = errors.New("no more items")
)
Functions ¶
Types ¶
type Iterator ¶
type Iterator[T any] interface { // Next returns the next result. Returns Done if there are no more results. A return of Done is idempotent // and multiple calls have no effect. // See usage example. Next() (T, error) }
Iterator defines the minimal contract for an Iterator pattern. Implementations must deallocate or close any open resource (e.g. file or network) on the first returned error. If not all entries are consumed, underlying finalizers will clean any open file descriptors, however implementations may provide an optional io.Closer for immediate release.
Example (Next) ¶
package main import ( "fmt" "github.com/golangee/repository/iter" ) func main() { var it iter.Iterator[string] for { item, err := it.Next() if err == iter.Done { break } if err != nil { // TODO: Handle error. } fmt.Println(item) } }
Output:
Example (Next2) ¶
package main import ( "fmt" "github.com/golangee/repository/iter" ) func main() { var it iter.Iterator[string] for item, err := it.Next(); err != iter.Done; { if err != nil { // TODO: Handle error. } fmt.Println(item) } }
Output:
Click to show internal directories.
Click to hide internal directories.