Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rangefunc support #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- run: go test -race -v ./...
- run: go test -race -v ./...
env:
GOEXPERIMENT: rangefunc
19 changes: 19 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ func ExampleReader_Event() {
// </root>
}

func ExampleReader_Events() {
xmlData := `<root><element>Value</element></root>`
reader := strings.NewReader(xmlData)

r := gosax.NewReader(reader)
for e, err := range r.Events {
if err != nil {
log.Fatal(err)
}
fmt.Println(string(e.Bytes))
}
// Output:
// <root>
// <element>
// Value
// </element>
// </root>
}

func ExampleNewReaderBuf() {
xmlData := `<root><element>Value</element></root>`
reader := strings.NewReader(xmlData)
Expand Down
17 changes: 17 additions & 0 deletions range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build (go1.22 && goexperiment.rangefunc) || go1.23

package gosax

// Events is a range function over all events, implementing [iter.Seq2].
// It yields each event in order (except [EventEOF]) and an error, if any is encountered during reading.
func (r *Reader) Events(yield func(Event, error) bool) {
for {
event, err := r.Event()
if event.Type() == EventEOF {
return
}
if !yield(event, err) {
return
}
}
}