How to Write a Custom Iterator in Go

Create a custom Go iterator by returning an iter.Seq function that yields values via a callback.

Write a function that returns iter.Seq[T] and uses a yield callback to emit values one by one.

func MyIterator(items []string) iter.Seq[string] {
	return func(yield func(string) bool) {
		for _, item := range items {
			if !yield(item) {
				return
			}
		}
	}
}

Use it with a for loop: for item := range MyIterator(data) { /* use item */ }.