Hello, I would definitely use *func Iter(ctx, data) iter.Seq2[T, error]*
Because its standard and it is easy for the end user to build a bridge to a scanner like API such as struct[T any]{ All() iter.Seq[T]; Err() error }, or* anything else*. Because i have make the *trailing error return* a *first citizen*, that signature fits straight into my own experiments . For certain, i would not use func IterWithErr(ctx, data, err *error) iter.Seq[T] It data raced the second i tried this approach. spaghetti for thoughts. https://go.dev/play/p/55w9IqH0KPQ Le mardi 3 septembre 2024 à 19:19:31 UTC+2, Christoph Berger a écrit : > I like this approach. > > - bufio.Scanner-style errors are a known pattern from the stdlib > - The approach keeps the key and value params free for their intended > use > - It doesn't require nested iter seqs > - It is compatible with range loops > > Made some sample code as PoC -> https://go.dev/play/p/6qhH6ZN6f0S (Doesn't > work in the playground due to network restrictions) > > On Saturday, August 31, 2024 at 1:38:53 PM UTC+2 gbarr wrote: > >> I would likely go with something similar to how you would currently use >> bufio.Scanner but comine the use of .Scan() and .Text() as Range() >> >> ```go >> iter := rows.Iter(ctx) >> for obj := iter.Range { >> // do something with obj >> } >> if err := iter.Err(); err != nil { >> return err >> } >> ``` >> >> Or if rows can only have a single active iterator the there is no need >> for the iter object >> >> ```go >> for obj := rows.Range(ctx) { >> // do something with obj >> } >> if err := rows.Err(); err != nil { >> return err >> } >> ``` >> >> Graham. >> >> On Thursday, August 29, 2024 at 1:41:47 PM UTC+1 Dmitriy P wrote: >> >>> What is the best way to handle errors with iterators? >>> >>> How to handle error when we have some object and some type paginates >>> data (e.g. database/sql.Rows or >>> https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#ListObjectsV2Paginator >>> )? >>> I found three, but which should be preferable or maybe I've missed some? >>> >>> ```go >>> type Object smth.Smth >>> type Paginator interface { >>> GetNext(ctx) ([]Object, error) // func doing heavy request over internet >>> HaveNext() bool >>> } >>> ``` >>> >>> ```go >>> func Iter(ctx, data) iter.Seq2[Object, error] >>> >>> for obj, err := Iter(...) { >>> if err != nil { >>> return err >>> } >>> // do smth with Object >>> } >>> ``` >>> >>> ```go >>> func IterWithErr(ctx, data, err *error) iter.Seq[Object] >>> >>> var err error >>> for obj := IterWithErr(..., &err) { >>> // do smth with Object >>> } >>> if err != nil { >>> return err >>> } >>> ``` >>> >>> ```go >>> func IterOverIter(ctx, data) iter.Seq2[iter.Seq[Object], error] >>> >>> for page, err := IterOverIter(...) { >>> if err != nil { >>> return err >>> } >>> for obj := page(...) { >>> // do smth with Object >>> } >>> } >>> ``` >>> >>> >>> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5e33b29d-ed4e-4399-ad93-d95483d519f0n%40googlegroups.com.