Fork me on GitHub

Applies a mapping function to each element of a source.

func Map[T any](mapper Mapper[T], source any, dest any) *Builder[T]
  • Description: Applies the mapper function to each element of source and stores the results in dest.

  • Parameters:

    • mapper: Function that transforms each element of type T into another type. It takes an element of type T and returns a transformed value of type any.

    • source: Source data to transform (expects pointers to slices or maps).

    • dest: Destination where mapped results will be stored (expects pointers to lists or maps).

Example

 1package main
 2import (
 3	"fmt"
 4	collection "github.com/jose78/go-collection/v2"
 5)
 6
 7func main() {
 8	slice := []int{1, 2, 3, 4, 5}
 9	var mappedSlice []string
10
11	builderError := collection.Map(func(elem int) any {
12		return fmt.Sprintf("Number %d", elem)
13	}, slice, &mappedSlice)
14
15	if builderError != nil {
16		fmt.Printf("Error during mapping: %v\n", builderError.Error())
17	} else {
18		fmt.Println("Mapped slice:", mappedSlice)
19	}
20}