Iterates over elements in a source and applies an action for each element.
func ForEach[K any](action Action[K], src any) *Builder[K]
Description: Iterates over elements of src and executes the action for each element.
Parameters:
action: Function defining the action to perform on each element. It takes an index (int) and an element of type K.
src: Source data to iterate over (expects pointers to slices or maps).
1package main
2
3import (
4 "fmt"
5
6 collection "github.com/jose78/go-collection/v2"
7)
8
9func actionPrint(idx, elem int) {
10 fmt.Printf("Index %d: %d\n", idx, elem)
11}
12
13func main() {
14 slice := []int{1, 2, 3, 4, 5}
15 builder := collection.ForEach(actionPrint, slice)
16
17 if builder != nil {
18 fmt.Printf("Error during iteration: %v\n", builder.Error())
19 }
20}