iterutil

package
v3.30.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package iterutil provides a set of iterutil for various data structures.

Elements of slices as pointers

The main motivation is making it easier to work with slices of large structures, where people often choose an iterator pattern, which makes a shallow copy of each struct in the slice:

var myStructs []MyStruct
for _, myStruct := range myStructs {
	// myStruct is a shallow *COPY* of each element - any changes in the loop have no effect on the slice element.
}

In order to improve that, and also not have to use the throw-away `_` variable, SlicePointerValues may be used to iterate across pointers to each element in the slice:

var myStructs []MyStruct
for myStructPtr := range iterutil.SlicePointerValues(myStructs) {
	// myStructPtr is a pointer to each element - any changes in the loop will be reflected in the slice element.
}

If you still need the index, you can use SlicePointers:

var myStructs []MyStruct
for i, myStructPtr := range iterutil.SlicePointers(myStructs) {
	// myStructPtr is a pointer to each element - any changes in the loop will be reflected in the slice element.
	// i is the index of the element.
}

Walking json

The https://github.com/tidwall/gjson library is already included as a dependency. If you need to walk a json document, the WalkGjsonLeaves iterator is available, which will yield the json paths to all leaves in the document along with the gjson.Result for each leaf.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Firsts

func Firsts[T, U any](seq2 iter.Seq2[T, U]) iter.Seq[T]

Firsts returns an iterator that yields the first elements of a sequence of pairs.

func Seconds

func Seconds[T, U any](seq2 iter.Seq2[T, U]) iter.Seq[U]

Seconds returns an iterator that yields the second elements of a sequence of pairs.

func SlicePointerValues

func SlicePointerValues[Slice ~[]T, T any](s Slice) iter.Seq[*T]

SlicePointerValues returns an iterator that yields pointers to the elements of a slice.

func SlicePointers

func SlicePointers[Slice ~[]T, T any](s Slice) iter.Seq2[int, *T]

SlicePointers returns an iterator that yields indices and pointers to the elements of a slice.

func WalkGjsonLeaves

func WalkGjsonLeaves(result gjson.Result) iter.Seq2[string, gjson.Result]

WalkGjsonLeaves returns an iterator of (path, result) from a gjson result; only leaves are yielded.

Types

type GjsonElem

type GjsonElem struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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