Fork me on GitHub

Combines two slices (keys and values) into a result map.

func Zip[K comparable, V any](keys []K, values []V, result map[K]V) *Builder[K]
  • Description: Combines elements from keys and values into the result map.

  • Parameters:

    • keys: Slice of keys for the resulting map.
    • values: Slice of values to associate with the keys.
    • result: Map where combined key-value pairs will be stored.

Example

 1package main
 2
 3import (
 4	"fmt"
 5
 6	collection "github.com/jose78/go-collection/v2"
 7)
 8
 9func main() {
10	keys := []string{"a", "b", "c"}
11	values := []int{1, 2, 3}
12	result := make(map[string]int)
13
14	builder := collection.Zip(keys, values, result)
15	if err := builder.Error(); err != nil {
16		fmt.Printf("Error during zipping: %v\n", err)
17	} else {
18		fmt.Println("Zipped map:", result)
19	}
20}